Home > Backend Development > XML/RSS Tutorial > XML—DOM4J for XML parsing

XML—DOM4J for XML parsing

黄舟
Release: 2017-02-24 15:14:51
Original
1580 people have browsed it


Why is there Dom4j in the first place?

Because DOM consumes too much memory, and SAX can only read XML, but cannot add, delete, etc. So Dom4j appeared, which is more efficient and can also perform crud operations.

1. Introduction to DOM4J

  • Dom4j is a simple, flexible open source library. Dom4j was split off from the early developers of JDOM and then developed independently. Unlike JDOM, dom4j uses interfaces and abstract base classes. Although Dom4j's API is relatively complex, it provides better flexibility than JDOM.

  • Dom4j is a very excellent Java XML API with excellent performance, powerful functions and extremely easy to use. Many software now use Dom4j, such as Hibernate, including Sun's own JAXP, which also uses Dom4j.

  • To develop using Dom4j, you need to download the corresponding jar file of dom4j and import it into the project. Download address dom4j download

2.DOM4J case

still use our previous XML file:

<?xml version="1.0" encoding="utf-8" standalone="no"?><班级>
    <学生 地址="香港">
        <名字>周小星</名字>
        <年龄>23</年龄>
        <介绍>学习刻苦</介绍>
    </学生>
    <学生 地址="澳门">
        <名字>林晓</名字>
        <年龄>25</年龄>
        <介绍>是一个好学生</介绍>
    </学生></班级>
Copy after login
Copy after login

This document is placed In the com.dom4j.test package.

Using DOM4J also requires obtaining the Document object representing the entire document, but this Document object is in the org.dom4j package.

Obtain the Document object in the main method as follows:

// 1.得到一个解析器SAXReader saxReader = new SAXReader();
// 2.指定解析哪个XML文件Document document = saxReader.read(new File("src/com/dom4j/test/myClass.xml"));
Copy after login
Copy after login

Then we can write the corresponding method according to the needs and call it in the main method.

【1】Specify to read an element (read the first student’s information)

public static void read(Document document) {    // 得到根元素
    Element root = document.getRootElement();    // root.elements("学生"):表示取出root下的所有学生元素
    // root.element("学生"):表示取出root下的第一个学生元素
    Element student = root.element("学生");    // 取出属性
    String address = student.attributeValue("地址");    // 取出各个子节点的值
    String name = student.element("名字").getText();
    String age = student.element("年龄").getText();
    String intro = student.element("介绍").getText();

    System.out.println(address);
    System.out.println(name);
    System.out.println(age);
    System.out.println(intro);
}
Copy after login
Copy after login

【2】Add element: Adding student information to the XML document

public static void add(Document document) throws Exception {    // 首先我们来创建一个学生节点对象
    Element student = DocumentHelper.createElement("学生");
    Element name = DocumentHelper.createElement("名字");
    name.setText("小强");
    Element age = DocumentHelper.createElement("年龄");
    age.setText("22");
    Element intro = DocumentHelper.createElement("介绍");
    intro.setText("是一个三好学生");    // 把三个子元素加到student节点下
    student.add(name);
    student.add(age);
    student.add(intro);    // 为学生添加属性
    student.addAttribute("地址", "大理");    // 将学生节点添加到根节点下
    document.getRootElement().add(student);    // 更新xml文件,直接输出会出现中文乱码,要用OutputFormat
    OutputFormat output = OutputFormat.createPrettyPrint();    // 设置输出的编码为utf-8
    output.setEncoding("utf-8");    // 这里一定要用FileOutputStream字节流输出,不能用FileWriter,否则还会有乱码
    XMLWriter xmlWriter = new XMLWriter(new FileOutputStream("src/com/dom4j/test/myClass.xml"), output);
    xmlWriter.write(document);
    xmlWriter.close();
}
Copy after login
Copy after login

To add students to the XML document, you also need to write the Document object in the memory to the corresponding file at the end, otherwise all operations are only performed in the memory. , and will not be output to a file, which is similar to DOM.
We can still write this updated code as a separate method, as follows:

public static void update(Document document) throws Exception {    // 更新xml文件,直接输出会出现中文乱码,要用OutputFormat
    OutputFormat output = OutputFormat.createPrettyPrint();    // 设置输出的编码为utf-8
    output.setEncoding("utf-8");
    XMLWriter xmlWriter = new XMLWriter(new FileOutputStream("src/com/dom4j/test/myClass.xml"), output);
    xmlWriter.write(document);
    xmlWriter.close();
}
Copy after login
Copy after login

[3] Add an element to the specified position

public static void addByIndex(Document document) throws Exception {    // 创建一个元素
    Element newStu = DocumentHelper.createElement("学生");
    newStu.setText("王小明");    // 得到所有学生的list
    List allStudent = document.getRootElement().elements("学生");
    allStudent.add(1, newStu);
    update(document);
}
Copy after login
Copy after login

Here, you can actually add it directly to the obtained List and then update it. The List here is the List


# in the

java.util package [4] Delete an element or delete this Element attributes
Deleting an element is similar to DOM, in that the node is deleted through the corresponding parent node. For example, we want to delete the first student node:

public static void delete(Document document) throws Exception {    // 找到该元素
    Element student = document.getRootElement().element("学生");    // 删除元素的某个属性
    student.remove(student.attribute("地址"));    // 通过父节点删除节点
    student.getParent().remove(student);
    update(document);
}
Copy after login
Copy after login

[5] Update element
For example, we want to add 1 to the age of all students, and add 1 to the age of all students. Change the address attribute to United States:

public static void updateAgeAndAddress(Document document) throws Exception {
Element root = document.getRootElement();
    List<Element> list = root.elements();    for (Element element : list) {        // 更新属性
        element.addAttribute("地址", "美国");        // 更新年龄子节点的值
        Element e_age = element.element("年龄");        int age = Integer.parseInt(e_age.getTextTrim());
        e_age.setText(String.valueOf(age + 1));
    }
    update(document);
}
Copy after login
Copy after login

Why is there Dom4j in the first place?

Because DOM consumes too much memory, and SAX can only read XML, but cannot add, delete, etc. So Dom4j appeared, which is more efficient and can also perform crud operations.

1. Introduction to DOM4J

  • Dom4j is a simple, flexible open source library. Dom4j was split off from the early developers of JDOM and then developed independently. Unlike JDOM, dom4j uses interfaces and abstract base classes. Although Dom4j's API is relatively complex, it provides better flexibility than JDOM.

  • Dom4j is a very excellent Java XML API with excellent performance, powerful functions and extremely easy to use. Many software now use Dom4j, such as Hibernate, including Sun's own JAXP, which also uses Dom4j.

  • To develop using Dom4j, you need to download the corresponding jar file of dom4j and import it into the project. Download address dom4j download

2.DOM4J case

still use our previous XML file:

<?xml version="1.0" encoding="utf-8" standalone="no"?><班级>
    <学生 地址="香港">
        <名字>周小星</名字>
        <年龄>23</年龄>
        <介绍>学习刻苦</介绍>
    </学生>
    <学生 地址="澳门">
        <名字>林晓</名字>
        <年龄>25</年龄>
        <介绍>是一个好学生</介绍>
    </学生></班级>
Copy after login
Copy after login

The document is placed In the com.dom4j.test package.

Using DOM4J also requires obtaining the Document object representing the entire document, but this Document object is in the org.dom4j package.

Obtain the Document object in the main method as follows:

// 1.得到一个解析器SAXReader saxReader = new SAXReader();
// 2.指定解析哪个XML文件Document document = saxReader.read(new File("src/com/dom4j/test/myClass.xml"));
Copy after login
Copy after login

Then we can write the corresponding method according to the needs and call it in the main method.

【1】Specify to read an element (read the first student’s information)

public static void read(Document document) {    // 得到根元素
    Element root = document.getRootElement();    // root.elements("学生"):表示取出root下的所有学生元素
    // root.element("学生"):表示取出root下的第一个学生元素
    Element student = root.element("学生");    // 取出属性
    String address = student.attributeValue("地址");    // 取出各个子节点的值
    String name = student.element("名字").getText();
    String age = student.element("年龄").getText();
    String intro = student.element("介绍").getText();

    System.out.println(address);
    System.out.println(name);
    System.out.println(age);
    System.out.println(intro);
}
Copy after login
Copy after login

【2】Add element: Add a student information to the XML document

public static void add(Document document) throws Exception {    // 首先我们来创建一个学生节点对象
    Element student = DocumentHelper.createElement("学生");
    Element name = DocumentHelper.createElement("名字");
    name.setText("小强");
    Element age = DocumentHelper.createElement("年龄");
    age.setText("22");
    Element intro = DocumentHelper.createElement("介绍");
    intro.setText("是一个三好学生");    // 把三个子元素加到student节点下
    student.add(name);
    student.add(age);
    student.add(intro);    // 为学生添加属性
    student.addAttribute("地址", "大理");    // 将学生节点添加到根节点下
    document.getRootElement().add(student);    // 更新xml文件,直接输出会出现中文乱码,要用OutputFormat
    OutputFormat output = OutputFormat.createPrettyPrint();    // 设置输出的编码为utf-8
    output.setEncoding("utf-8");    // 这里一定要用FileOutputStream字节流输出,不能用FileWriter,否则还会有乱码
    XMLWriter xmlWriter = new XMLWriter(new FileOutputStream("src/com/dom4j/test/myClass.xml"), output);
    xmlWriter.write(document);
    xmlWriter.close();
}
Copy after login
Copy after login

To add a student to the XML document, you also need to write the Document object in the memory to the corresponding file at the end, otherwise all operations are only performed in the memory. , and will not be output to a file, which is similar to DOM.
We can still write this updated code as a separate method, as follows:

public static void update(Document document) throws Exception {    // 更新xml文件,直接输出会出现中文乱码,要用OutputFormat
    OutputFormat output = OutputFormat.createPrettyPrint();    // 设置输出的编码为utf-8
    output.setEncoding("utf-8");
    XMLWriter xmlWriter = new XMLWriter(new FileOutputStream("src/com/dom4j/test/myClass.xml"), output);
    xmlWriter.write(document);
    xmlWriter.close();
}
Copy after login
Copy after login

[3] Add an element to the specified position

public static void addByIndex(Document document) throws Exception {    // 创建一个元素
    Element newStu = DocumentHelper.createElement("学生");
    newStu.setText("王小明");    // 得到所有学生的list
    List allStudent = document.getRootElement().elements("学生");
    allStudent.add(1, newStu);
    update(document);
}
Copy after login
Copy after login

Here, you can actually add it directly to the obtained List and then update it. The List here is the List


# in the

java.util package [4] Delete an element or delete this Element attributes
Deleting an element is similar to DOM, in that the node is deleted through the corresponding parent node. For example, we want to delete the first student node:

public static void delete(Document document) throws Exception {    // 找到该元素
    Element student = document.getRootElement().element("学生");    // 删除元素的某个属性
    student.remove(student.attribute("地址"));    // 通过父节点删除节点
    student.getParent().remove(student);
    update(document);
}
Copy after login
Copy after login

[5] Update element
For example, we want to add 1 to the age of all students, and add 1 to the age of all students. Change the address attribute to United States:

public static void updateAgeAndAddress(Document document) throws Exception {
Element root = document.getRootElement();
    List<Element> list = root.elements();    for (Element element : list) {        // 更新属性
        element.addAttribute("地址", "美国");        // 更新年龄子节点的值
        Element e_age = element.element("年龄");        int age = Integer.parseInt(e_age.getTextTrim());
        e_age.setText(String.valueOf(age + 1));
    }
    update(document);
}
Copy after login
Copy after login

 以上就是XML—XML解析之DOM4J的内容,更多相关内容请关注PHP中文网(www.php.cn)!


Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
objective-c - socket sends data in xml format
From 1970-01-01 08:00:00
0
0
0
How to parse and process HTML/XML in PHP?
From 1970-01-01 08:00:00
0
0
0
How to parse and process HTML/XML in PHP?
From 1970-01-01 08:00:00
0
0
0
How to parse and process HTML/XML using PHP?
From 1970-01-01 08:00:00
0
0
0
Update xml namespace with data from PHP form
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template