dom4j for XML parsing

1. Parse XML using dom4j technology

public class Dom4jTest {
    public static void main(String[] args) {
        //1.创建SAXReader对象
        SAXReader saxReader=new SAXReader();

        try {
             // 2. Load the book.xml file through the SAXReader object and get the Document object 
            Document document = saxReader.read( new File("/home/yaming/Desktop/workspace/javatech-parent/xml/src/main/resources/ books.xml" ));
             // 3. Get the root node Element through the getRootElement method 
            bookStore =   document.getRootElement();
             // 4. Get the iterator (the first level child node of the root node) through elementIterator 
            Iterator iterator= bookStore. elementIterator();
             // 5. Traverse the iterator to get the information under the root node (child nodes of the root node) 
            while (iterator.hasNext()){
                System.out.println( "------------Start traversing a certain book---------" );
                Element book = (Element) iterator.next(); // Force type conversion
                 // (1). Get the attribute name and attribute value of the book through attributes (child nodes under child nodes) 
                List<Attribute> bookAttrs= book.attributes ();
                 for (Attribute attr:bookAttrs){
                     // Get attribute name 
                    String name= attr.getName();
                     // Get attribute value 
                    String value= attr.getValue();
                    System.out.println( "Property name: "+name+" Property value: "+ value);
                }

                // (2). Get the node name and node value of the child node of the book node through elementIterator 
                Iterator it= book.elementIterator();
                 while (it.hasNext()){
                    Element bookChild = (Element) it.next();
                     // Get node name 
                    String name= bookChild.getName();
                     // Get node value 
                    String value= bookChild.getStringValue();
                    System.out.println( "Node name: "+name+"Node value: "+ value);
                }
                System.out.println( "------------Start traversing a certain book---------" );
            }
        } catch (DocumentException e) {
            e.printStackTrace ();
        }

    }
}

2. Description:

#1 . Introduce jar package
 < !-- https://mvnrepository.com/artifact/dom4j/dom4j -->
        <dependency>
            <groupId>dom4j</groupId>
            <artifactId>dom4j</artifactId>
            <version>1.6.1</version>
        </dependency> 
# 2. Programming
##1.
Create a SAXReader object
##2.
Load the book.xml file through the SAXReader object and get the Document object
##3.
Get the root node through the getRootElement method
##4.
Get the iterator (child nodes of the root node) through elementIterator
##5.
Traverse the iterator to get the information under the root node (child nodes of the root node)
####(1).
 Get the attribute name and attribute value of the book through attributes (the child node under the child node)
 ####(2)
 Get the node name and node value of the child node of the book node through elementIterator

3. Other technologies for parsing xml, dom, jdom, sax, etc. make up later

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324681102&siteId=291194637