dom4j操作xml的demo(分享)

2025-05-29 0 27

废话不多说,直接上代码

?

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的文本内容来创建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); */

/*

* // 获取一个节点 Element element = root.element("title");

*

*

* //获取element的id属性节点对象 Attribute attr = element.attribute("id");

* //删除属性 element.remove(attr);

*

* // 添加新属性 element.addAttribute("author", "作者");

*

* // 添加新的节点 Element newElement = root.addElement("where"); //

* 设定新节点的值 newElement.setText("北京人民出版社,天津人民大学出版社");

*

* // 获取element中的where元素节点对象 Element author =

* element.element("where"); // 删除元素节点 boolean flag =

* element.remove(author); // 返回true代码删除成功,否则失败

* System.out.println(flag); // 添加CDATA区域

* element.addCDATA("红楼梦,是一部爱情小说."); // 写入到一个新的文件中 writer(document);

*/

} catch (DocumentException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

/*

*

* 所有节点内容

*/

public static void getElement(Element root) {

// 获取当前节点的所有属性节点

List<Attribute> as = root.attributes();

for (Attribute a : as) {

System.out.println("当前属性节点的名称:" + a.getName());

/*

* System.out.println("当前属性节点的内容:" + a.getText());

*

* System.out.println("当前属性节点的值:" + a.getValue());

*/

}

if (!root.getTextTrim().equals("")) {

System.out.println("文本内容::::" + root.getText());

}

Iterator<Element> el = root.elementIterator();

while (el.hasNext()) {

// 获取某个子节点对象

Element e = el.next();

// 对子节点进行遍历

getElement(e);

}

}

/**

* 介绍Element中的element方法和elements方法的使用

*

* @param node

*/

public static void elementMethod(Element node) {

// 获取node节点中,子节点的元素名称为西游记的元素节点。

Element e = node.element("info");

// 获取西游记元素节点中,子节点为chapter的元素节点(可以看到只能获取第一个作者元素节点)

Element author = e.element("classification");

System.out.println(e.getName() + "----" + author.getText());

// 获取西游记这个元素节点 中,所有子节点名称为classification元素的节点 。

List<Element> authors = e.elements("classification");

for (Element aut : authors) {

System.out.println(aut.getText());

}

// 获取西游记这个元素节点 所有元素的子节点。

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();

// 设置编码

format.setEncoding("UTF-8");

// 创建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();

// 关闭操作

writer.close();

}

// 以下的代码为字符串与xml互转实例

public void test() throws Exception {

// 创建saxreader对象

SAXReader reader = new SAXReader();

// 读取一个文件,把这个文件转换成Document对象

Document document = reader.read(new File("src//c.xml"));

// 获取根元素

Element root = document.getRootElement();

// 把文档转换字符串

String docXmlText = document.asXML();

System.out.println(docXmlText);

System.out.println("---------------------------");

// csdn元素标签根转换的内容

String rootXmlText = root.asXML();

System.out.println(rootXmlText);

System.out.println("---------------------------");

// 获取java元素标签 内的内容

Element e = root.element("java");

System.out.println(e.asXML());

}

/**

* 创建一个document对象 往document对象中添加节点元素 转存为xml文件

*

* @throws Exception

*/

public void test2() throws Exception {

Document document = DocumentHelper.createDocument();// 创建根节点

Element root = document.addElement("csdn");

Element java = root.addElement("java");

java.setText("java班");

Element ios = root.addElement("ios");

ios.setText("ios班");

writer(document);

}

/**

* 把一个文本字符串转换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();

// 设置编码

format.setEncoding("UTF-8");

// 创建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();

// 关闭操作

writer.close();

}

public static void main(String[] args) {

try {

anaXml();

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

以上这篇dom4j操作xml的demo(分享)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持快网idc。

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 dom4j操作xml的demo(分享) https://www.kuaiidc.com/115943.html

相关文章

发表评论
暂无评论