当前位置:网站首页>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>
边栏推荐
- matlab神經網絡所有傳遞函數(激活函數)公式詳解
- Markdown learning
- Osgearth target selection
- JS ternary operator - learning notes (with cases)
- 使用base64编码传图片
- Creation and content of mapnode -- osgearth rendering engine series (2)
- 【Rust 笔记】09-特型与泛型
- [concurrent programming] collaboration between threads
- Cloudcompare learning (1) - cloudcompare compilation and common plug-in implementation
- Osganimation library parsing
猜你喜欢
[concurrent programming] concurrent tool class of thread
22-06-27 西安 redis(01) 安装redis、redis5种常见数据类型的命令
Markdown learning
Final review of Database Principles
[RPC] RPC remote procedure call
了解小程序的笔记 2022/7/3
Alibaba canaladmin deployment and canal cluster Ha Construction
Graphics_ Learnopongl learning notes
[cloud native] introduction and use of feign of microservices
Vscode, idea, VIM development tool shortcut keys
随机推荐
Use of ue5 QRcode plug-in
Notes on understanding applets 2022/7/3
[K & R] Chinese Second Edition personal questions Chapter1
[concurrent programming] Table hopping and blocking queue
Downward compatibility and upward compatibility
Intersectionpicker in osgearth
【Rust笔记】05-错误处理
Data analysis exercises
Ue5 opencv plug-in use
Message pack in C deserializes array objects
LinkedList set
[concurrent programming] working mechanism and type of thread pool
Collection interface
[cloud native] introduction and use of feign of microservices
Kwai 20200412 recruitment
Solution détaillée de toutes les formules de fonction de transfert (fonction d'activation) du réseau neuronal MATLAB
数据库原理期末复习
图像处理8-CNN图像分类
Clion toolchains are not configured configure disable profile problem solving
matlab神經網絡所有傳遞函數(激活函數)公式詳解