[XML parsing] (4) Using DOM4J parsing to parse XML documents in Java

Keywords: xml Attribute Java Spring

DOM4J source:

Dom4j(Document For Java) - Third-party open source, is a parsing technology that is separated from jdom. At present, JDOM has been completely replaced by dom4j.

[Note]

(1) The predecessor of jDom-Dom4j.

(2) Dom4j is faster in performance and speed than sun and supports Xpath fast lookup

At present, large frameworks like Spring and Hibernate are all dom4j.

(4) Supporting document reading and writing function and Xpath fast query operation.

[DOM4J Open Source Package Directory]

How to learn DOM4J?

/**
*
* Learning methods:
* 1) Open docs/index.html -> Quick Start -> in the dom4j project package. This page describes the basic operation of dom4j
* 2) index.html -> Javadoc (1.6.1) - > Enter the API document of this version, and you can see the description of classes and methods in each package.
* 3) official website: http://www.dom4j.org/
*
*/

Detailed illustration::

dom4j-1.6.1/docs/index.html:

Quick start:

★★From here on, we can learn about dom4j★★:

Parsing XML,

Using Iterators,

Powerful Navigation with XPath,

Fast Looping,

Creating a new XML document,

Writing a document to a file,

Converting to and from Strings,

Styling a Document with XSLT

Javadoc:

Here we can use JOM4J API, the key core technology can be found here, start learning API bar!

Start parsing xml documents with dom4j:

With this simple understanding, you can parse xml documents using dom4j technology.

Example: Let's start parsing the users.xml document:

The xml document to parse:

<?xml version="1.0" encoding="UTF-8"?>
<users> 
  <user id="A001"> 
    <name>Jack</name>  
    <age>22</age> 
  </user>  
  <user id="A002"> 
    <name>Zhang San</name>  
    <age>24</age> 
  </user>  
  <user id="B001"> 
    <name>petty thief</name>  
    <age>20</age> 
  </user>  
  <user id="B002"> 
    <name>Xiao Zhang</name>  
    <age>28</age> 
  </user>  
  <user id="0777"> 
    <name>Hunan City University</name>  
    <age>1200</age> 
  </user>  


</users>

(1) How to obtain DOM objects through dom4j?

Core code:

SAXReader sax = new SAXReader();
Document dom = sax.read("./xml/users.xml");

[Note]: Package used: org.dom4j.io

With packages, here we derive the class hierarchy of DOM4J::

[Special attention]: Because of previous learning of DOM parsing, it is particularly easy to confuse org.w3c.dom with the org.dom4j.io package. In future Dom4j parsing programming, Node (node), Element (element), Document (document) are all important content obtained from dom4j.

(2) How to obtain sub-elements?

What is a child element?

The child elements of dom include:

Mainly: element(), elements()

(1) To obtain a single sub-element, the following methods are used:

element(String name),

(2) Obtain all the elements by the following methods:

elements(): List
elements(String name): List

(3) Individual elements or all elements can also be obtained by iterators:

elementIterator(): Iterator
elementIterator(String name): Iterator

Code demo:

public void helloDom4j() throws DocumentException {
    // Getting DOM objects
    SAXReader sax = new SAXReader();
    Document dom = sax.read("./xml/users.xml");

    // Get the root element
    Element root = dom.getRootElement();
    // Get the name of the root element
    String rootName = root.getName();
    System.out.println(rootName);
    /*
     * Some ways to get the child elements under the root element: root.elements()// Get all the child elements java.util.List
     * root.element(qName)//Gets the first element of the specified name, root.elements(qName)// Gets all the child elements of the specified name
     * root.elementIterator()//Get all the elements -- iterators
     * root.elementIterator(qName)//Gets all child elements of the specified name
     */

    // Requirements: Extract the first <user> information
    Element eUser = root.element("user");
    String id = eUser.attributeValue("id");
    String name = eUser.element("name").getTextTrim();
    String age = eUser.elementText("age");
    System.out.println(id + "," + name + "," + age);// Results: A001,Jack,22
}

Operation results:

Use DOM4J to add, delete and modify XML documents:

With the above foundation, you can do a series of operations on XML documents!

(1) Increase - Increase the last element

// increase---stay root Add one below<hncu>node
@Test
public void add() throws DocumentException, Exception {
    SAXReader sax = new SAXReader();
    Document dom = sax.read("./xml/users.xml");
    Element root = dom.getRootElement();
    // Add node
    Element eHncu = root.addElement("hncu");
    eHncu.addAttribute("id", "0737");
    Element eName = eHncu.addElement("name");
    eName.addText("hunan city university");
    Element eAge = eHncu.addElement("age");
    eAge.addText("15");

    // ////////////////////The following demo saves//////////////////////////////
    // Preservation----Persistence----Character stream to be brushed
    // method1: Simple way-----Pass directly Document One of them writer(writer w)To achieve
    /*
     * FileWriter fw=new FileWriter("./xml/users.xml/"); dom.write(fw);
     * fw.close();
     */
    // method2: Ordinary way-----adopt XMLwriter One of them writer(dom)
    /*
     * 2.1Constructing XMLWriter(OutputStream) XMLWriter xw =new XMLWriter(new
     * FileOutputStream("./xml/users.xml")); xw.write(dom); xw.close();
     */
    /*
     * 2.2Constructing XMLWriter(Writer writer) XMLWriter xw =new XMLWriter(new
     * FileWriter("./xml/users.xml/")); xw.write(dom); xw.close();
     */
    /*
     * 2.3Constructing XML Writer (Writer w, Output Format)
     * format)----Output Format, which can solve the problem of Chinese random code, can be output in a format.
     * format=OutputFormat.createPrettyPrint(); XMLWriter xw=new
     * XMLWriter(new FileOutputStream("./xml/users.xml/"), format);
     * xw.write(dom); xw.close();
     */
    OutputFormat format = OutputFormat.createPrettyPrint();
    XMLWriter xw = new XMLWriter(new FileOutputStream("./xml/users.xml/"),
            format);
    xw.write(dom);
    xw.close();
}

(2) Delete - Delete the last element

// Delete - Delete the last element
@Test
public void del() throws DocumentException, IOException {
    SAXReader sax = new SAXReader();
    Document dom = sax.read("./xml/users.xml");
    Element root = dom.getRootElement();
    List<Element> list = root.elements();
    Element e = list.get(list.size() - 1);
    e.getParent().remove(e);
    // Preservation
    XMLWriter xw = new XMLWriter(new FileOutputStream("./xml/users.xml"));
    xw.write(dom);
    xw.close();
}

(3) Change - modify the last element

// Change - modify the last element
@Test
public void update() throws Exception {
    SAXReader sax = new SAXReader();
    Document dom = sax.read("./xml/users.xml");
    Element root=dom.getRootElement();
    List<Element> list=root.elements();
    Element eLast =list.get(list.size()-1);
    eLast.addAttribute("id", "0777");
    eLast.element("name").setText("Hunan City University");
    eLast.element("age").setText("1200");
    XMLWriter xw=new XMLWriter(new FileOutputStream("./xml/users.xml"));
    xw.write(dom);
    xw.close();

}

(4) Check - traverse all

// Check - traverse all < user >
@Test
public void queryAll() throws DocumentException {
    SAXReader sax = new SAXReader();
    Document dom = sax.read("./xml/users.xml/");
    Element root = dom.getRootElement();
    Iterator<Element> it = root.elementIterator();
    while (it.hasNext()) {
        Element eUser = it.next();
        String id = eUser.attributeValue("id");
        String name = eUser.elementText("name");
        String age = eUser.elementText("age");
        System.out.println(id + "," + name + "," + age);
    }
}

Posted by Goofan on Thu, 20 Dec 2018 22:48:05 -0800