当前位置:网站首页>dom4j解析xml
dom4j解析xml
2022-06-10 11:57:00 【wfsm】
jar包
<!-- https://mvnrepository.com/artifact/org.dom4j/dom4j -->
<dependency>
<groupId>org.dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>jaxen</groupId>
<artifactId>jaxen</artifactId>
<version>1.2.0</version>
</dependency>
xml
<?xml version="1.0" encoding="UTF-8"?>
<books>
<book>
<author>Thomas</author>
<title>Java从入门到放弃</title>
<publisher>UCCU</publisher>
</book>
<book>
<author>小白</author>
<title>MySQL从删库到跑路</title>
<publisher>Go Die</publisher>
</book>
<book>
<author>PHPer</author>
<title>Best PHP</title>
<publisher>PHPchurch</publisher>
</book>
</books>
方式一:root.elements()
public static void main(String[] args) throws DocumentException {
// 获取当前项目路径
String url = System.getProperty("user.dir");
System.out.println("url = " + url);
SAXReader reader = new SAXReader();
File file = new File(url + File.separator + "1.xml");
Document fileDocument = reader.read(file);
// 获取根节点
Element root = fileDocument.getRootElement();
// 获取根节点下所有子元素
List<Element> childElements = root.elements();
for (Element child : childElements) {
List<Element> books = child.elements();
for (Element book : books) {
// 元素名
String name = book.getName();
// 元素值
String text = book.getText();
System.out.println(name+":"+text);
}
}
Element book1 = childElements.get(0);
Element author = book1.element("author");
System.out.println("author is "+author.getText());
}
方式二:document.selectNodes("路径下的节点")
public static void main(String[] args) throws DocumentException, IOException {
String url = System.getProperty("user.dir");
SAXReader reader = new SAXReader();
File file = new File(url + File.separator + "1.xml");
Document document = reader.read(file);
List<Node> authors = document.selectNodes("/books/book/author");
if (!CollectionUtils.isEmpty(authors)){
authors.iterator().forEachRemaining(item-> System.out.println(item.getText()));
}
Element root = document.getRootElement();
// 必须在根节点下添加
Element phone = root.addElement("phone");
phone.addText("123");
boolean book = root.remove(root.element("book"));
System.out.println("book = " + book);
// 将修改后的文档,写入xml中
FileWriter fileWriter = new FileWriter(System.getProperty("user.dir") + File.separator + "1.xml");
XMLWriter writer = new XMLWriter(fileWriter);
writer.write(document);
writer.close();
fileWriter.close();
}
方法三:迭代器遍历
//1.创建Reader对象
SAXReader reader = new SAXReader();
//2.加载xml
Document document = reader.read(new File(pomXml));
//3.获取根节点
Element rootElement = document.getRootElement();
Iterator iterator = rootElement.elementIterator();
问题:
getText()和getStringValue()区别:getText()返回当前节点文本内容 ,,getStringValue()返回当前节点的子孙节点所有文本内容连接成的字符串
https://blog.csdn.net/qq_35873847/article/details/77711559
边栏推荐
- Using SQLite3 database in embedded Linux
- Tmux使用
- 'getColor(int)' is deprecated ,getColor过时
- 【限时】,干货满满
- 'getDrawable(int)' is deprecated,getDrawable过时
- The securities and futures industry meets the new data regulation, and IP guard helps improve data security management
- La poignée d'enseignement de la station B vous apprend à utiliser le masque yolov5 pour tester les éléments de l'enregistrement le plus complet (apprentissage profond / détection d'objets / pythorch)
- CLIP使用
- 在启牛开户证券安全吗
- 嵌入式Linux中使用SQLite3数据库
猜你喜欢

共筑数字经济可信底座|2022可信数据库峰会即将召开

"Duty" is waiting for you| Book planning and editing (Conference Proceedings)

Remote access to tensorboard

How to compile product marketing plan

西骏数据与阿里云携手合作,获得阿里云产品集成认证!

期末考试——编译原理

Building a trusted base for digital economy 𞓜 2022 trusted database summit will be held soon

Transfomer各组件与Pytorch

业余不求人,30秒拖拽制作封面图

web服务器开发,小型公司web开发
随机推荐
现场勘察制度
2022 年 5 月产品大事记
掌握高性能计算前,我们先了解一下它的历史
Do not concatenate text displayed with setText,use resource string with placeholders.
更改系统时间
Product milestones in May 2022
0509-130 Symbol resolution failed for /oracle/app/oracle/12.2.0/db_ 1/lib/libons. So problem handling
Web server development, small company web development
Transfomer自实现与官方库
C语言最佳实践之库文件介绍(下)
Introduction to the library of C language best practices (Part 1)
[yuntu Said] chaque système d'affaires réussi ne peut pas être séparé de l'escorte de l'apig
线性代数的本质5 行列式
陆金所:对外技术输出赋能,助力金融企业实现数据库自主可控
你对PHP数据类型或者其他编程语言的数据类型了解多少呢
Do not concatenate text displayed with setText,use resource string with placeholders.
SQL Server AlwaysOn查看数据同步进度
"Yi +" open source program - Design and practice of low delay player based on webrtc
The essence of linear algebra 4 matrix multiplication and linear compound transformation
Do you have an online stock account? Is it safe to open an account online?