⽤dom4j修改xml(增加修改节点)
博客分类:
使⽤dom4j修改解析xml,xml⽂件的位置是配置在xml.properties⽂件中,程序打成jar包,在命令⾏中执⾏如下命令即可:[img]dl.iteye/upload/attachment/150593/6b4ac972-cf45-3582-9a04-5e88bd0c962d.jpg" alt="[/img] 主要的java代码如下:
Java代码
1. import java.io.BufferedInputStream;
2. import java.io.File;
3. import java.io.FileInputStream;
4. import java.io.FileOutputStream;
5. import java.io.IOException;
6. import java.io.InputStream;
7. import java.util.Iterator;
8. import java.util.List;
9. import java.util.Properties;
10.
11. import javax.servlet.http.HttpServlet;
12.
13. import org.dom4j.Document;
14. import org.dom4j.DocumentException;
15. import org.dom4j.Element;
16. import org.dom4j.Node;
17. import org.dom4j.io.SAXReader;
18. import org.dom4j.io.XMLWriter;
19.
20.
21. public class RefreshXml extends HttpServlet {
22. private String xmlFilePath="";
23.
24. public void modifyXml(String xmlProperty) throws DocumentException, IOException {
25. Properties(xmlProperty);
26. File file = new File(xmlFilePath);
27. SAXReader reader = new SAXReader();
28. Document doc = ad(file);
29. List list;
30.
31. /*
32. * 修改EntityProps
33. */
使用dom4j解析xml文件34. list = doc.selectNodes("//Entity_Groups//Entity//EntityProps");
35. System.out.println("需要修改的EntityProps⼀共有:"+list.size()+"处。");
36. for(int i=0;i<list.size();i++){
37. Element elt = (Element) (i);
38. Node node = elt.selectSingleNode("Definition");
39. if(node == null){
40. Element info = elt.addElement("Definition");
41. info.addAttribute("xml:space", "preserve");
42. info.addText(elt.selectSingleNode("Name").getText());
43. // elt.selectSingleNode("Definition").setText(elt.selectSingleNode("Name").getText());
44. }else{
45. elt.selectSingleNode("Definition").setText(elt.selectSingleNode("Name").getText());
46. }
47.
48. }
49.
50. /*
51. * 修改AttributeProps
52. */
53. list = doc.selectNodes("//Entity_Groups//Entity//Attribute_Groups//Attribute//AttributeProps");
54. System.out.println("需要修改的AttributeProps⼀共有:"+list.size()+"处。");
55. for(int i=0;i<list.size();i++){
56. Element elt = (Element) (i);
57. Node node = elt.selectSingleNode("Definition");
58. if(node == null){
59. //增加节点
60. Element info = elt.addElement("Definition");
61. info.addAttribute("xml:space", "preserve");
62. info.addText(elt.selectSingleNode("Name").getText());
63. // elt.selectSingleNode("Definition").setText(elt.selectSingleNode("Name").getText());
64. }else{
65. elt.selectSingleNode("Definition").setText(elt.selectSingleNode("Name").getText());
66. }
67. }
68. System.out.println("修改完毕!");
69.
70. XMLWriter writer = new XMLWriter(new FileOutputStream(file));
71. writer.write(doc);
72. writer.close();
73.
74. }
75.
76. private void getProperties(String xmlProperty) throws IOException{
77. //String configFile = xmlProperty;
78. String configFile = "D:/Workspaces for MyEclipse 7.1/RefreshXml/src/xml.properties";
79. InputStream is = new BufferedInputStream(new FileInputStream(new File(configFile)));
80. if(is == null){
81. is = ServletContext().getResourceAsStream(configFile);
82. }
83. Properties ps = new Properties();
84. ps.load(is);
85. xmlFilePath = ps.getProperty("xmlPath");
86. }
87.
88. /**
89. * @param args
90. */
91. public static void main(String[] args) {
92. if (args.length < 1) {
93. System.out.println("请输⼊程序所需的配置⽂件路径作为参数:");
94. System.out.println("1、要修改的xml⽂件的名称。");
95. } else {
96. String xmlProperty = args[0];
97. System.out.println(xmlProperty);
98. RefreshXml rx = new RefreshXml();
99. try {
100. rx.modifyXml(xmlProperty);
101. } catch (DocumentException e) {
102. e.printStackTrace();
103. } catch (IOException e) {
104. e.printStackTrace();
105. }
106. }
107. }
108. }
配置⽂件xml.properties中的内容也⽐较简单:
xmlPath=C\:\\test\\l
如下是程序jar包:
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论