当前位置:网站首页>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>
边栏推荐
- [concurrent programming] atomic operation CAS
- matlab神經網絡所有傳遞函數(激活函數)公式詳解
- P1596 [USACO10OCT]Lake Counting S
- [rust note] 10 operator overloading
- [rust notes] 02 ownership
- 单调栈-503. 下一个更大元素 II
- Clion toolchains are not configured configure disable profile problem solving
- Life cycle of Servlet
- Talking about: is the HashSet set ordered or disordered /hashset set unique, why can we store elements with the same content
- 【Rust 笔记】09-特型与泛型
猜你喜欢

XPath实现XML文档的查询

Campus lost and found platform based on SSM, source code, database script, project import and operation video tutorial, Thesis Writing Tutorial

Life cycle of Servlet

Deep parsing JVM memory model

单调栈-84. 柱状图中最大的矩形

Notes on understanding applets 2022/7/3

Simple demo of solving BP neural network by gradient descent method

详解sizeof、strlen、指针和数组等组合题

Thymeleaf 404 reports an error: there was unexpected error (type=not found, status=404)

Mxone Pro adaptive 2.0 film and television template watermelon video theme apple cmsv10 template
随机推荐
Talking about: is the HashSet set ordered or disordered /hashset set unique, why can we store elements with the same content
How does unity fixedupdate call at a fixed frame rate
Deep parsing (picture and text) JVM garbage collector (II)
Redis的数据结构
Deep parsing JVM memory model
100 GIS practical application cases (78) - Multi compliance database design and data warehousing
Analysis of Alibaba canal principle
Jupyter remote server configuration and server startup
Cloudcompare learning (1) - cloudcompare compilation and common plug-in implementation
[rust notes] 05 error handling
【Rust 笔记】11-实用特型
Unity learning notes
[audio and video] ijkplayer error code
Collection interface
Alibaba canal actual combat
第一个Servlet
Redis cluster series 4
Unity editor expansion - scrolling list
[concurrent programming] collaboration between threads
Unity Editor Extension - drag and drop