廢話不多說,直接上代碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
|
package com.cn.shop.util; import java.io.File; import java.io.FileOutputStream; import java.io.OutputStreamWriter; import java.util.Iterator; import java.util.List; import org.dom4j.Attribute; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.io.OutputFormat; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; /** * * @author NH * */ public class XmlUtils { public static Document getDocument() { // 1.讀取xml文件獲取document對象 SAXReader reader = new SAXReader(); Document document = null; try { document = reader.read("D:\\itext\\27663.xml"); } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } // 2.通過解析xml的文本 /* * String xmlFilePath = "D:\\itext\\27663.xml"; try { document = * DocumentHelper.parseText(xmlFilePath); } catch (DocumentException e) * { // TODO Auto-generated catch block e.printStackTrace(); } // 3.通過 * Document document = DocumentHelper.createDocument(); Element root = * document.addElement("csdn"); */ return document; } public static void anaXml() throws Exception { // 讀取xml的文本內(nèi)容來創(chuàng)建document對象 SAXReader reader = new SAXReader(); try { Document document = reader.read("D:\\itext\\27663.xml"); Element root = document.getRootElement(); System.out.println(root.getName()); getElement(root); /* elementMethod(root); */ /* * // 獲取一個節(jié)點 Element element = root.element("title"); * * * //獲取element的id屬性節(jié)點對象 Attribute attr = element.attribute("id"); * //刪除屬性 element.remove(attr); * * // 添加新屬性 element.addAttribute("author", "作者"); * * // 添加新的節(jié)點 Element newElement = root.addElement("where"); // * 設(shè)定新節(jié)點的值 newElement.setText("北京人民出版社,天津人民大學(xué)出版社"); * * // 獲取element中的where元素節(jié)點對象 Element author = * element.element("where"); // 刪除元素節(jié)點 boolean flag = * element.remove(author); // 返回true代碼刪除成功,否則失敗 * System.out.println(flag); // 添加CDATA區(qū)域 * element.addCDATA("紅樓夢,是一部愛情小說."); // 寫入到一個新的文件中 writer(document); */ } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /* * * 所有節(jié)點內(nèi)容 */ public static void getElement(Element root) { // 獲取當(dāng)前節(jié)點的所有屬性節(jié)點 List< Attribute > as = root.attributes(); for (Attribute a : as) { System.out.println("當(dāng)前屬性節(jié)點的名稱:" + a.getName()); /* * System.out.println("當(dāng)前屬性節(jié)點的內(nèi)容:" + a.getText()); * * System.out.println("當(dāng)前屬性節(jié)點的值:" + a.getValue()); */ } if (!root.getTextTrim().equals("")) { System.out.println("文本內(nèi)容::::" + root.getText()); } Iterator< Element > el = root.elementIterator(); while (el.hasNext()) { // 獲取某個子節(jié)點對象 Element e = el.next(); // 對子節(jié)點進行遍歷 getElement(e); } } /** * 介紹Element中的element方法和elements方法的使用 * * @param node */ public static void elementMethod(Element node) { // 獲取node節(jié)點中,子節(jié)點的元素名稱為西游記的元素節(jié)點。 Element e = node.element("info"); // 獲取西游記元素節(jié)點中,子節(jié)點為chapter的元素節(jié)點(可以看到只能獲取第一個作者元素節(jié)點) Element author = e.element("classification"); System.out.println(e.getName() + "----" + author.getText()); // 獲取西游記這個元素節(jié)點 中,所有子節(jié)點名稱為classification元素的節(jié)點 。 List< Element > authors = e.elements("classification"); for (Element aut : authors) { System.out.println(aut.getText()); } // 獲取西游記這個元素節(jié)點 所有元素的子節(jié)點。 List< Element > elements = e.elements(); for (Element el : elements) { System.out.println(el.getText()); } } /** * 把document對象寫入新的文件 * * @param document * @throws Exception */ public static void writer(Document document) throws Exception { // 緊湊的格式 // OutputFormat format = OutputFormat.createCompactFormat(); // 排版縮進的格式 OutputFormat format = OutputFormat.createPrettyPrint(); // 設(shè)置編碼 format.setEncoding("UTF-8"); // 創(chuàng)建XMLWriter對象,指定了寫出文件及編碼格式 /* * XMLWriter writer = new XMLWriter(new OutputStreamWriter(new * FileOutputStream(new File("src//a.xml")), "UTF-8"), format); */ File file = new File("c://index//大主宰.xml"); FileOutputStream fos = new FileOutputStream(file); OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8"); XMLWriter writer = new XMLWriter(osw); // 寫入 writer.write(document); // 立即寫入 writer.flush(); // 關(guān)閉操作 writer.close(); } // 以下的代碼為字符串與xml互轉(zhuǎn)實例 public void test() throws Exception { // 創(chuàng)建saxreader對象 SAXReader reader = new SAXReader(); // 讀取一個文件,把這個文件轉(zhuǎn)換成Document對象 Document document = reader.read(new File("src//c.xml")); // 獲取根元素 Element root = document.getRootElement(); // 把文檔轉(zhuǎn)換字符串 String docXmlText = document.asXML(); System.out.println(docXmlText); System.out.println("---------------------------"); // csdn元素標(biāo)簽根轉(zhuǎn)換的內(nèi)容 String rootXmlText = root.asXML(); System.out.println(rootXmlText); System.out.println("---------------------------"); // 獲取java元素標(biāo)簽 內(nèi)的內(nèi)容 Element e = root.element("java"); System.out.println(e.asXML()); } /** * 創(chuàng)建一個document對象 往document對象中添加節(jié)點元素 轉(zhuǎn)存為xml文件 * * @throws Exception */ public void test2() throws Exception { Document document = DocumentHelper.createDocument();// 創(chuàng)建根節(jié)點 Element root = document.addElement("csdn"); Element java = root.addElement("java"); java.setText("java班"); Element ios = root.addElement("ios"); ios.setText("ios班"); writer(document); } /** * 把一個文本字符串轉(zhuǎn)換Document對象 * * @throws Exception */ public void test1() throws Exception { String text = "< csdn >< java >Java班</ java >< net >Net班</ net ></ csdn >"; Document document = DocumentHelper.parseText(text); Element e = document.getRootElement(); System.out.println(e.getName()); writer(document); } /** * 把document對象寫入新的文件 * * @param document * @throws Exception */ public void writer1(Document document) throws Exception { // 緊湊的格式 // OutputFormat format = OutputFormat.createCompactFormat(); // 排版縮進的格式 OutputFormat format = OutputFormat.createPrettyPrint(); // 設(shè)置編碼 format.setEncoding("UTF-8"); // 創(chuàng)建XMLWriter對象,指定了寫出文件及編碼格式 // XMLWriter writer = new XMLWriter(new FileWriter(new // File("src//a.xml")),format); XMLWriter writer = new XMLWriter(new OutputStreamWriter(new FileOutputStream(new File("src//c.xml")), "UTF-8"), format); // 寫入 writer.write(document); // 立即寫入 writer.flush(); // 關(guān)閉操作 writer.close(); } public static void main(String[] args) { try { anaXml(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } |
以上這篇dom4j操作xml的demo(分享)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持服務(wù)器之家。