首页 > 开发 > Java > 正文

java使用Jdom实现xml文件写入操作实例

2024-07-13 09:55:58
字体:
来源:转载
供稿:网友

这篇文章主要介绍了java使用Jdom实现xml文件写入操作的方法,以完整实例形式分析了Jdom针对XML文件写入操作的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了java使用Jdom实现xml文件写入操作的方法。分享给大家供大家参考,具体如下:

 

  1. package com.yanek.demo.xml.test; 
  2. import java.io.File; 
  3. import java.io.FileWriter; 
  4. import org.jdom.Attribute; 
  5. import org.jdom.Document; 
  6. import org.jdom.Element; 
  7. import org.jdom.input.SAXBuilder; 
  8. import org.jdom.output.XMLOutputter; 
  9. public class JdomWriteXml { 
  10. /** 
  11. * @param args 
  12. */ 
  13. public static void main(String[] args) { 
  14. SAXBuilder sb = new SAXBuilder(); 
  15. Element actions = new Element("actions"); 
  16. Document document = new Document(actions); 
  17. Element action1 = new Element("action"); 
  18. actions.addContent(action1); 
  19. Attribute path_atbt1 = new Attribute("path""/test"); 
  20. Attribute class_atbt1 = new Attribute("class"
  21. "com.mystruts.demo.LoginAction"); 
  22. action1.setAttribute(path_atbt1); 
  23. action1.setAttribute(class_atbt1); 
  24. Element action1_forward1 = new Element("forward"); 
  25. action1.addContent(action1_forward1); 
  26. Attribute action1_forward1_name_atbt1 = new Attribute("name""success"); 
  27. Attribute action1_forward1_url_atbt1 = new Attribute("url""test.jsp"); 
  28. action1_forward1.setAttribute(action1_forward1_name_atbt1); 
  29. action1_forward1.setAttribute(action1_forward1_url_atbt1); 
  30. Element action1_forward2 = new Element("forward"); 
  31. action1.addContent(action1_forward2); 
  32. Attribute action1_forward1_name_atbt2 = new Attribute("name""failure"); 
  33. Attribute action1_forward1_url_atbt2 = new Attribute("url"
  34. "failure.jsp"); 
  35. action1_forward2.setAttribute(action1_forward1_name_atbt2); 
  36. action1_forward2.setAttribute(action1_forward1_url_atbt2); 
  37. Element action2 = new Element("action"); 
  38. actions.addContent(action2); 
  39. Attribute path_atbt2 = new Attribute("path""/user"); 
  40. Attribute class_atbt2 = new Attribute("class"
  41. "com.mystruts.demo.UserAction"); 
  42. action2.setAttribute(path_atbt2); 
  43. action2.setAttribute(class_atbt2); 
  44. Element action2_forward1 = new Element("forward"); 
  45. action2.addContent(action2_forward1); 
  46. Attribute action2_forward1_name_atbt1 = new Attribute("name""success"); 
  47. Attribute action2_forward1_url_atbt1 = new Attribute("url""test.jsp"); 
  48. action2_forward1.setAttribute(action2_forward1_name_atbt1); 
  49. action2_forward1.setAttribute(action2_forward1_url_atbt1); 
  50. Element action2_forward2 = new Element("forward"); 
  51. action2.addContent(action2_forward2); 
  52. Attribute action2_forward1_name_atbt2 = new Attribute("name""failure"); 
  53. Attribute action2_forward1_url_atbt2 = new Attribute("url"
  54. "failure.jsp"); 
  55. action2_forward2.setAttribute(action2_forward1_name_atbt2); 
  56. action2_forward2.setAttribute(action2_forward1_url_atbt2); 
  57. Attribute root_atbt1 = new Attribute("m""001"); 
  58. actions.setAttribute(root_atbt1); 
  59. try { 
  60. File f1 = new File("mystruts.xml"); 
  61. // XMLOutputter xo=new XMLOutputter(" ",true,"GB2312"); 
  62. XMLOutputter xo = new XMLOutputter(); 
  63. FileWriter fw = new FileWriter(f1); 
  64. xo.output(document, fw); 
  65. fw.close(); 
  66. catch (Exception e) { 
  67. e.printStackTrace(); 
  68. // System.out.println(document.toString()); 

生成xml文件:

 

 
  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <actions m="001"
  3. <action path="/test" class="com.mystruts.demo.LoginAction"
  4. <forward name="success" url="test.jsp" /> 
  5. <forward name="failure" url="failure.jsp" /> 
  6. </action> 
  7. <action path="/user" class="com.mystruts.demo.UserAction"
  8. <forward name="success" url="test.jsp" /> 
  9. <forward name="failure" url="failure.jsp" /> 
  10. </action> 
  11. </actions> 

希望本文所述对大家Java程序设计有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表