当前位置:网站首页>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
- 【Rust 笔记】11-实用特型
- Unity editor expansion - scrolling list
- [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
- Graphics_ Games101/202 learning notes
- Ue5 opencv plug-in use
- 【Rust 笔记】13-迭代器(上)
- [concurrent programming] concurrent tool class of thread
- [redis] redis persistent RDB vs AOF (source code)
- Dom4j遍历和更新XML
猜你喜欢

Use of ue5 QRcode plug-in
![[redis] redis persistent RDB vs AOF (source code)](/img/57/b6a86c49cedee31fc00dc5d1372023.jpg)
[redis] redis persistent RDB vs AOF (source code)

Allocation exception Servlet

Dealing with duplicate data in Excel with xlwings

Notes on understanding applets 2022/7/3

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

Introduction to Base64 coding

请求参数的发送和接收

php-fpm软件的安装+openresty高速缓存搭建

Alibaba canal actual combat
随机推荐
【Rust 笔记】13-迭代器(上)
[concurrent programming] concurrent security
Use of ue5 QRcode plug-in
[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
[concurrent programming] concurrent tool class of thread
单调栈-42. 接雨水
Display terrain database on osgearth ball
[public key cryptography] ECC elliptic cryptosystem (implementing ElGamal encryption method)
图像处理8-CNN图像分类
Introduction to Base64 coding
分配异常的servlet
Mxone Pro adaptive 2.0 film and television template watermelon video theme apple cmsv10 template
Data analysis exercises
Notes on understanding applets 2022/7/3
Collection interface
[MySQL] MySQL Performance Optimization Practice: introduction of database lock and index search principle
[rust notes] 05 error handling
matlab神经网络所有传递函数(激活函数)公式详解
VIM learning notes from introduction to silk skating
Downward compatibility and upward compatibility