首页 归档 关于 文件 Github
×

XML操作之dom4j

2023-07-07 11:16:12
J-Tools
  • Xml
本文总阅读量(次):
本文字数统计(字):426
本文阅读时长(分):2

依赖

1
2
3
4
5
6
7
8
9
10
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>jaxen</groupId>
<artifactId>jaxen</artifactId>
<version>1.2.0</version>
</dependency>

dom4j:解析,生成
jaxen:查找节点

读取

XML样例

1
2
3
4
5
6
7
8
9
10
11
<root>
<head>
<accessToken>token</accessToken>
</head>
<body>
<request>
<param id='123' age='18'>张三</param>
<param id='456' age='20'>李四</param>
</request>
</body>
</root>

读取xml文件

1
2
Document document = new SAXReader().read(filePath);
Element rootElement = document.getRootElement();

读取xml字符串

1
2
Document document = DocumentHelper.parseText(xmlStr);
Element rootElement = document.getRootElement();

解析

解析属性

1
String attributeKey = rootElement.attributeValue("attributeKey");

解析下级节点

1
Element ele = rootElement.element("elementName")

迭代下级节点(多个)

1
2
3
4
5
Iterator<Element> rootIter = root.elementIterator();	
while (rootIter.hasNext()) {
Element ele = rootIter.next();
System.out.println(ele.getName());
}

查找

1
2
单节点:String accessToken = rootElement.selectSingleNode("/root/head/accessToken").getStringValue();   
多节点:List param = root.selectNodes("/root/body/request/param");

生成

生成xml文档

1
2
3
4
5
6
7
8
9
10
11
// 创建一个Document实例
Document document = DocumentHelper.createDocument();
Element rootEle = document.addElement("root");

Element headEle = rootEle.addElement("head");
headEle.addElement("accessToken").addText("token");

Element bodyEle = rootEle.addElement("body");
Element requestEle = bodyEle.addElement("request");
requestEle.addElement("param").addAttribute("id","123").addAttribute("age","18").addText("张三");
requestEle.addElement("param").addAttribute("id","124").addAttribute("age","20").addText("李四");

输出xml字符串

1
String str = document.asXML();

输出xml文件

展开格式:OutputFormat format = OutputFormat.createPrettyPrint();
压缩格式:OutputFormat format = OutputFormat.createCompactFormat();

1
2
3
4
5
6
7
// 自定义xml样式
OutputFormat format = new OutputFormat();
format.setEncoding("UTF-8");
format.setNewLineAfterDeclaration(false); // 放置xml文件中第二行为空白行
// 输出xml文件
XMLWriter writer = new XMLWriter(new FileOutputStream(filePath), format);
writer.write(document);
完
绿色版Tomcat注册Windows服务并开机自启
json数据的转换(Jackson)

本文标题:XML操作之dom4j

文章作者:十二

发布时间:2023-07-07 11:16:12

最后更新:2024-11-13 17:08:08

原始链接:https://www.zhuqiaolun.com/2023/07/1688699772517/1688699772517/

许可协议:署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

头像

十二

我想起那天夕阳下的奔跑,那是我逝去的青春。

分类

  • Blog4
  • ElasticSearch13
  • Git2
  • Go-FastDfs2
  • IDEA2
  • J-Package6
  • J-Tools21
  • Java2
  • JavaFx6
  • Kafka4
  • Linux2
  • Logger6
  • Maven5
  • MyBatis6
  • MyCat3
  • MySql2
  • Nginx5
  • OceanBase1
  • RabbitMq4
  • Redis6
  • SVN1
  • SpringBoot16
  • Tomcat6
  • WebService2
  • Windows2
  • kubernetes10

归档

  • 一月 20261
  • 十二月 20253
  • 八月 20252
  • 六月 20251
  • 二月 20251
  • 十二月 20244
  • 八月 202416
  • 六月 20241
  • 九月 20231
  • 八月 20231
  • 七月 20232
  • 八月 20222
  • 三月 202214
  • 二月 20224
  • 十一月 20211
  • 七月 20215
  • 六月 20213
  • 五月 20213
  • 四月 20211
  • 三月 202116
  • 二月 20212
  • 一月 20211
  • 十一月 202014
  • 十月 20201
  • 九月 202014
  • 八月 20205
  • 七月 20204
  • 六月 20208
  • 五月 20208

作品

我的微信 我的文件

网站信息

本站运行时间统计: 载入中...
本站文章字数统计:103.2k
本站文章数量统计:139
© 2026 十二  |  鄂ICP备18019781号-1  |  鄂公网安备42118202000044号
驱动于 Hexo  | 主题 antiquity  |  不蒜子告之 阁下是第个访客
首页 归档 关于 文件 Github