??1
package
?dom;
??2
??3
import
?java.io.File;
??4
import
?java.io.FileWriter;
??5
??6
import
?org.dom4j.Document;
??7
import
?org.dom4j.DocumentHelper;
??8
import
?org.dom4j.Element;
??9
import
?org.dom4j.Node;
?10
import
?org.dom4j.io.SAXReader;
?11
import
?org.dom4j.io.XMLWriter;
?12
?13
/**?*/
/**
?14
?*?
@author
??~@_@
?15
?
*/
?16
public
?
class
?Dom4jDemo?
{
?17
????
/**?*/
/**
?XML文件路徑?
*/
?18
????
private
?String?XMLPath?
=
?
null
;
?19
?20
????
/**?*/
/**
?XML文檔?
*/
?21
????
private
?Document?document?
=
?
null
;
?22
?23
????
public
?Dom4jDemo()?
{
?24
????}
?25
?26
????
/**?*/
/**
?27
?????*??初始化xml文件
?28
?????*?
?29
?????*?
@param
?XMLPath?文件路徑
?30
?????
*/
?31
????
public
?Dom4jDemo(String?XMLPath)?
{
?32
????????
this
.XMLPath?
=
?XMLPath;
?33
????}
?34
?35
????
/**?*/
/**
?*?打開文檔?
*/
?36
????
public
?
void
?openXML()?
{
?37
????????
try
?
{
?38
????????????SAXReader?reader?
=
?
new
?SAXReader();
?39
????????????
this
.document?
=
?reader.read(
this
.XMLPath);
?40
????????????System.out.println(
"
openXML()?successful?
"
);
?41
????????}
?
catch
?(Exception?e)?
{
?42
????????????System.out.println(
"
openXML()?Exception:
"
?
+
?e.getMessage());
?43
????????}
?44
????}
?45
?46
????
/**?*/
/**
?47
?????*??創建文檔
?48
?????*?
?49
?????*?
@param
?rootName?根節點名稱
?50
?????
*/
?51
????
public
?
void
?createXML(String?rootName)?
{
?52
????????
try
?
{
?53
????????????
this
.document?
=
?DocumentHelper.createDocument();
?54
????????????Element?root?
=
?document.addElement(rootName);
?55
????????????System.out.println(
"
createXML()?successful
"
);
?56
????????}
?
catch
?(Exception?e)?
{
?57
????????????System.out.println(
"
createXML()?Exception:
"
?
+
?e.getMessage());
?58
????????}
?59
????}
?60
?61
????
/**?*/
/**
?62
?????*??添加根節點的child
?63
?????*?
?64
?????*?
@param
?nodeName?節點名
?65
?????*?
@param
?nodeValue?節點值
?66
?????
*/
?67
????
public
?
void
?addNodeFromRoot(String?nodeName,?String?nodeValue)?
{
?68
????????Element?root?
=
?
this
.document.getRootElement();
?69
????????Element?level1?
=
?root.addElement(nodeName);
?70
????????level1.addText(nodeValue);
?71
????}
?72
?73
????
/**?*/
/**
?74
?????*??打開文檔
?75
?????*?
?76
?????*?
@param
?filePath?文檔路徑
?77
?????
*/
?78
????
public
?
void
?openXML(String?filePath)?
{
?79
????????
try
?
{
?80
????????????SAXReader?saxReader?
=
?
new
?SAXReader();
?81
????????????
this
.document?
=
?saxReader.read(filePath);
?82
????????????System.out.println(
"
openXML(String?filePath)?successful?
"
);
?83
????????}
?
catch
?(Exception?e)?
{
?84
????????????System.out.println(
"
openXML()?Exception:
"
?
+
?e.getMessage());
?85
????????}
?86
????}
?87
?88
????
/**?*/
/**
?保存文檔?
*/
?89
????
public
?
void
?saveXML()?
{
?90
????????
try
?
{
?91
????????????XMLWriter?output?
=
?
new
?XMLWriter(
new
?FileWriter(
new
?File(
?92
????????????????????
this
.XMLPath)));
?93
????????????output.write(document);
?94
????????????output.close();
?95
????????????System.out.println(
"
saveXML()?successful?
"
);
?96
????????}
?
catch
?(Exception?e1)?
{
?97
????????????System.out.println(
"
saveXML()?Exception:
"
?
+
?e1.getMessage());
?98
????????}
?99
????}
100
101
????
/**?*/
/**
102
?????*??保存文檔
103
?????*?
104
?????*?
@param
?toFilePath?保存路徑
105
?????
*/
106
????
public
?
void
?saveXML(String?toFilePath)?
{
107
????????
try
?
{
108
????????????XMLWriter?output?
=
?
new
?XMLWriter(
new
?FileWriter(
109
????????????????????
new
?File(toFilePath)));
110
????????????output.write(document);
111
????????????output.close();
112
????????}
?
catch
?(Exception?e1)?
{
113
????????????System.out.println(
"
saveXML()?Exception:
"
?
+
?e1.getMessage());
114
????????}
115
????}
116
117
????
/**?*/
/**
118
?????*??獲得某個節點的值
119
?????*?
120
?????*?
@param
?nodeName?節點名稱
121
?????
*/
122
????
public
?String?getElementValue(String?nodeName)?
{
123
????????
try
?
{
124
????????????Node?node?
=
?document.selectSingleNode(
"
//
"
?
+
?nodeName);
125
????????????
return
?node.getText();
126
????????}
?
catch
?(Exception?e1)?
{
127
????????????System.out
128
????????????????????.println(
"
getElementValue()?Exception:
"
?
+
?e1.getMessage());
129
????????????
return
?
null
;
130
????????}
131
????}
132
133
????
/**?*/
/**
134
?????*??獲得某個節點的子節點的值
135
?????*?
136
?????*?
@param
?nodeName
137
?????*?
@param
?childNodeName
138
?????*?
@return
139
?????
*/
140
????
public
?String?getElementValue(String?nodeName,?String?childNodeName)?
{
141
????????
try
?
{
142
????????????Node?node?
=
?
this
.document.selectSingleNode(
"
//
"
?
+
?nodeName?
+
?
"
/
"
143
????????????????????
+
?childNodeName);
144
????????????
return
?node.getText();
145
????????}
?
catch
?(Exception?e1)?
{
146
????????????System.out
147
????????????????????.println(
"
getElementValue()?Exception:
"
?
+
?e1.getMessage());
148
????????????
return
?
null
;
149
????????}
150
????}
151
152
????
/**?*/
/**
153
?????*??設置一個節點的text
154
?????*?
155
?????*?
@param
?nodeName?節點名
156
?????*?
@param
?nodeValue?節點值
157
?????
*/
158
????
public
?
void
?setElementValue(String?nodeName,?String?nodeValue)?
{
159
????????
try
?
{
160
????????????Node?node?
=
?
this
.document.selectSingleNode(
"
//
"
?
+
?nodeName);
161
????????????node.setText(nodeValue);
162
????????}
?
catch
?(Exception?e1)?
{
163
????????????System.out
164
????????????????????.println(
"
setElementValue()?Exception:
"
?
+
?e1.getMessage());
165
????????}
166
????}
167
168
????
/**?*/
/**
169
?????*??設置一個節點值
170
?????*?
171
?????*?
@param
?nodeName?父節點名
172
?????*?
@param
?childNodeName?節點名
173
?????*?
@param
?nodeValue?節點值
174
?????
*/
175
????
public
?
void
?setElementValue(String?nodeName,?String?childNodeName,
176
????????????String?nodeValue)?
{
177
????????
try
?
{
178
????????????Node?node?
=
?
this
.document.selectSingleNode(
"
//
"
?
+
?nodeName?
+
?
"
/
"
179
????????????????????
+
?childNodeName);
180
????????????node.setText(nodeValue);
181
????????}
?
catch
?(Exception?e1)?
{
182
????????????System.out
183
????????????????????.println(
"
setElementValue()?Exception:
"
?
+
?e1.getMessage());
184
????????}
185
????}
186
}

??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
