本文實例為大家分享了java使用dom4j對xml文件進行增刪改查操作的具體代碼,供大家參考,具體內(nèi)容如下
源代碼:
dom4j.java
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
|
package com.zc.homework19; import java.io.filewriter; import java.util.arraylist; import java.util.iterator; import java.util.list; import org.dom4j.document; import org.dom4j.element; import org.dom4j.io.saxreader; import org.dom4j.io.xmlwriter; import com.zc.homework19.book; public class dom4j { public static void main(string args[]) throws exception { /** * 第一步,得到document對象。 */ document document = getdocument(); /** * 第二步,修改得到的document對象 */ /** * 首先,讀取功能 */ list<book> books = readallelementsfromxmldocument(document); traversebooks(books); /** * 其次,修改功能 修改內(nèi)容:將id為b002的元素的title改為java core,price改為100.01 */ modifyinformationofxmldocument(document); /** * 再者:實現(xiàn)刪除功能 刪除內(nèi)容:刪除掉id為book1的元素內(nèi)容。 */ deleteinformationofxmldocument(document); /** * 最后:實現(xiàn)添加i新元素功能 添加內(nèi)容:id為book3,title內(nèi)容為:鳳姐玉照,price內(nèi)容為10000.00 */ addnewbooktoxmldocument(document); /** * 第三步:將得到的document對象持久化保存到硬盤(xml) */ writetonewxmldocument(document); } /** * 實現(xiàn)了添加新節(jié)點:book的功能 * * @param document */ private static void addnewbooktoxmldocument(document document) { element root = document.getrootelement(); element newbook = root.addelement( "book" ); newbook.addattribute( "id" , "book3" ); element java" id="highlighter_175934">
book.xml
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。 原文鏈接:https://blog.csdn.net/zc2274460/article/details/51182587 延伸 · 閱讀
精彩推薦
|