Detailed explanation of DOM4J in java object-oriented programming 3

Details of DOM4J in java object-oriented programming 3 (7)

 

1, Introduction & overview

Itcast tools: fast, too many restrictions

dom4j: free, more code than itcast tools

 

Environment construction:

 

 

 

2, Getting started: parsing XML files

Three steps of parsing: (purpose: get the root tag)

 

/*
* dom4j Introduction case:
*   Read the root tag name and age properties
*
*   attributeValue("Property name '); gets the property value of the specified name under the current label
*   getName()                      Get the name of the current label
* */
public static void main(String[] args) throws Exception {
    //1. Three steps of parsing (purpose: get the root tag)
    //1.1. Create SAX parser (for reading XML file)
    SAXReader reader = new SAXReader();
    //1.2. Read the XML file and return the DOM object through the parser (read the XML file, encapsulate the whole XML into a DOM object)
    Document dom = reader.read(new File("demo.xml"));
    //1.3. Get the root tag through DOM object
    //  Get root Element / get root label Element --- label
    Element root = dom.getRootElement();

    //Requirement 1: get the root tag list name attribute
    String name = root.attributeValue("name");
    System.out.println(name);
    //Requirement 2: get the name of the root tag
    String name1 = root.getName();
    System.out.println(name1);
}
 

 

 

 

3, Common methods

  1. SaxReader object
    1. read(... )Load execution xml document
  2. Document object
    1. getRootElement() gets the root element
  3. Element object
    • elements(... )Gets all child elements of the specified name. No name can be specified
    • element(... )Gets the first child element of the specified name. No name can be specified
    • getName() gets the element name of the current element
    • attributeValue(… )Gets the property value of the specified property name
    • elementText(… )Gets the text value of the specified name child element
    • getText() gets the text content of the current element

 

/*
*   dom4j Common methods:
*   1. SaxReader object
        a) read(...) Load execution xml document
    2. Document object
        a) getRootElement() Get root element
    3. Element Objects (each label is an element object)
        a) elements(...) Gets all child elements of the specified name. No name can be specified
        b) element(...) Gets the first child element of the specified name. No name can be specified
        c) getName() Get the element name of the current element
        d) attributeValue(...) Gets the property value of the specified property name
        e) elementText(...) Gets the text value of the specified name child element
        f) getText() Get the text content of the current element
* */
public static void main(String[] args) throws Exception {
    //1. Three steps of analysis
    //1.1. Create SAX parser
    SAXReader reader = new SAXReader();
    //1.2. Read XML file and DOM object through parser
    Document dom = reader.read(new File("demo.xml"));
    //1.3. Through DOM object, root tag
    Element root = dom.getRootElement();

    //Requirement 1: get the classes label dom4j: get the children through the parent
    Element classes = root.element("classes");
    System.out.println(classes.getName());
    //Requirement 2: get the content body text of the name sub label in the classes
    //1.1. Get name tag
    Element name = classes.element("name");
    //1.2. Get the content text of name label
    String text = name.getText();
    System.out.println(text);
    //Requirement 3: get the content body text of the name sub label in the classes
    String n = classes.elementText("name");
    System.out.println(n);
    //Requirement 4: get all the sub tags (stu,stu,classes) under the root tag
    List<Element> list = root.elements();
    System.out.println(list.size());
    //Requirement 5: get all stu sub tags under the root tag
    List<Element> list2 = root.elements("stu");
    System.out.println(list2.size());
}

 

 

 

 

4, Analytic principle

XML parsing principle:

DOM: read the whole XML document at one time, and encapsulate an XML document as a document object (DOM object)

 

 

 

Advantage: DOM parsing can read and write

Disadvantage: DOM parsing cannot load large XML file

 

SAX: it's a faster and more effective way. It scans the document line by line, scanning and parsing at the same time.

The specific analysis is carried out in an event driven way, and the corresponding event will be triggered for each line. (understanding)

 

Advantage: load and parse large XML

Disadvantage: unable to write data

 

Dom4j: combining the advantages of SAX and DOM

SAX parser parsing DOM tree

 

 

 

 

Please give yourself a compliment!

Make a little progress every day`~~~~~

Tags: Programming xml Java Attribute

Posted on Sat, 28 Mar 2020 10:04:25 -0700 by fr@nkie