当前位置:网站首页>Dom4j traverses and updates XML
Dom4j traverses and updates XML
2022-07-03 08:45:00 【The corner of fufu】
Catalog
One 、Dom4j
1、DOM Document object model
DOM(Document Object Model) Defines access and operations XML Standard approach to documentation ,DOM hold XML The document is viewed as a tree structure , Can pass DOM Tree to read and write all elements .
2、Dom4j
Dom4j It's an easy to use 、 Open source library , For parsing XML, It is used for Java platform .
Dom4j take XML As Document object .
XML The label is Dom4j Defined as Element object .
Two 、Dom4j Traverse XML
xml page
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hr SYSTEM "hr.dtd">
<!-- Human resource management system -->
<hr>
<employee no="3309">
<name> Zhang San </name>
<age>31</age>
<salary>5000</salary>
<department>
<dname> The administration department </dname>
<address>xx building -B103</address>
</department>
</employee>
<employee no="3310">
<name> Li Si </name>
<age>29</age>
<salary>8000</salary>
<department>
<dname> Technology Department </dname>
<address>xx building -B104</address>
</department>
</employee>
</hr>
java page
package com.imooc.dom4j;
import java.util.List;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class HrReader{
public void readXml() {
String file="d:/workplace/xml/src/hr.xml";
// SAXReader Class is read XML Core classes of files , Is used to XML Resolve to “ Trees ” Form preservation of
SAXReader reader = new SAXReader();
try {
Document document = reader.read(file);
// obtain XML The root node of the document , namely hr label
Element root = document.getRootElement();
// elements Method is used to get the specified set of tags
List<Element> employees = root.elements("employee");
for(Element employee : employees) {
// element Method is used to get the unique child node object
Element name = employee.element("name");
String empName = name.getText();// getText() Method is used to get the label text
System.out.println(empName);
System.out.println(employee.elementText("age"));
System.out.println(employee.elementText("salary"));
Element department = employee.element("department");
System.out.println(department.element("dname").getText());
System.out.println(department.element("address").getText());
Attribute att = employee.attribute("no");
System.out.println(att.getText()+"\n");
}
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
HrReader reader = new HrReader();
reader.readXml();
}
}
Output page 
3、 ... and 、Dom4j to update XML
java page
package com.imooc.dom4j;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class HrWriter {
public void writeXml() {
String file = "d:/workplace/xml/src/hr.xml";
SAXReader reader = new SAXReader();
try {
Document document = reader.read(file);
Element root = document.getRootElement();
Element employee = root.addElement("employee");
employee.addAttribute("no","3311");
Element name = employee.addElement("name");
name.setText(" Wang Wu ");
employee.addElement("age").setText("24");
employee.addElement("salary").setText("3600");
Element department = employee.addElement("department");
department.addElement("dname").setText(" The personnel department ");
department.addElement("address").setText("XX building -B205");
Writer writer = new OutputStreamWriter(new FileOutputStream(file),"UTF-8");
document.write(writer);
writer.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
HrWriter hrWriter = new HrWriter();
hrWriter.writeXml();
}
}
xml Change the page
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hr SYSTEM "hr.dtd"><!-- Human resource management system -->
<hr>
<employee no="3309">
<name> Zhang San </name>
<age>31</age>
<salary>5000</salary>
<department>
<dname> The administration department </dname>
<address>xx building -B103</address>
</department>
</employee>
<employee no="3310">
<name> Li Si </name>
<age>29</age>
<salary>8000</salary>
<department>
<dname> Technology Department </dname>
<address>xx building -B104</address>
</department>
</employee>
<employee no="3311">
<name> Wang Wu </name>
<age>24</age>
<salary>3600</salary>
<department>
<dname> The personnel department </dname>
<address>XX building -B205</address>
</department>
</employee>
</hr>
边栏推荐
- Intersectionpicker in osgearth
- Deep parsing JVM memory model
- Explain sizeof, strlen, pointer, array and other combination questions in detail
- Osgearth target selection
- 基于SSM的校园失物招领平台,源码,数据库脚本,项目导入运行视频教程,论文撰写教程
- Drawing maze EasyX library with recursive backtracking method
- Unity editor expansion - controls, layouts
- 【Rust 笔记】12-闭包
- [concurrent programming] thread foundation and sharing between threads
- Campus lost and found platform based on SSM, source code, database script, project import and operation video tutorial, Thesis Writing Tutorial
猜你喜欢
![[RPC] RPC remote procedure call](/img/dc/872204ea47fcff04cdb72e18a2a4ef.jpg)
[RPC] RPC remote procedure call

Redux - learning notes
![[redis] redis persistent RDB vs AOF (source code)](/img/57/b6a86c49cedee31fc00dc5d1372023.jpg)
[redis] redis persistent RDB vs AOF (source code)

Monotonic stack -84 The largest rectangle in the histogram

分配异常的servlet

基于SSM的校园失物招领平台,源码,数据库脚本,项目导入运行视频教程,论文撰写教程

Data analysis exercises

22-06-27 西安 redis(01) 安装redis、redis5种常见数据类型的命令

Advanced OSG collision detection

了解小程序的笔记 2022/7/3
随机推荐
How to deal with the core task delay caused by insufficient data warehouse resources
Osgearth topographic shading map drawing
Redis data structure
UE4 plug in development
Clion toolchains are not configured configure disable profile problem solving
JS ternary operator - learning notes (with cases)
梯度下降法求解BP神经网络的简单Demo
Deep parsing (picture and text) JVM garbage collector (II)
Osgearth north arrow display
[rust notes] 05 error handling
[set theory] order relation (total order relation | total order set | total order relation example | quasi order relation | quasi order relation theorem | bifurcation | quasi linear order relation | q
XPath实现XML文档的查询
[redis] redis persistent RDB vs AOF (source code)
UE4 source code reading_ Bone model and animation system_ Animation compression
详解sizeof、strlen、指针和数组等组合题
UE4 call DLL
Markdown directory generation
【Rust笔记】02-所有权
Life cycle of Servlet
数据库原理期末复习