hello.xml


      
<bookstore>
    <book id="1">    <! -- id="1"-- attribute name and attribute value -->
        <name>The Demi-Gods and the Semi-Devils</name>
        <author>Jin yong</author>
        <year>2014</year>
        <price>88</price>
    </book>
    <book id="2">
        <name>The deer and the cauldron</name>
        <year>2015</year>
        <price>66</price>
        <language>Chinese</language>
    </book>
    <book id="3">
        <name>The Legend of The Condor Heroes</name>
        <author>Jin yong</author>
        <year>2016</year>
        <price>44</price>
    </book> 
</bookstore>
Copy the code

Document Object Model (DOM) parsing

advantages

  1. Allows applications to make changes to data and structure
  2. Access is two-way, and you can navigate up and down the tree to get and manipulate any part of the data at any time

disadvantages

Parsing XML documents requires loading the entire document to construct the hierarchy, which consumes a lot of memory resources.

Range of application

Traversal is powerful and is often used in services where XML documents need to change frequently.

Analytical steps

  1. Create a DocumentBuilderFactory object
  2. Create a DocumentBuilder object
  3. Through the DocumentBuilderparse()Method to load XML into the current project directory
  4. throughgetElementsByTagName()Method to get a collection of all nodes for all XML
  5. Traverse all nodes
  6. throughitem()Method to get the properties of a node
  7. throughgetNodeName()getNodeValue()Method to get the property name and property value
  8. throughgetChildNodes()Method to get the child nodes and traverse all the child nodes
  9. throughgetNodeName()getTextContent()Method To obtain the child node name and value
package Paint;

import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class DOMTest {
    public static void main(String[] args) {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        try {
            DocumentBuilder db = dbf.newDocumentBuilder(); 
            Document document = db.parse("./src/Paint/hello.xml");  
            NodeList bookList = document.getElementsByTagName("book");	/ / the node set

            int bookCnt = bookList.getLength();
            System.err.println("All of them." + bookCnt +"Book");

            for(int i=0; i<bookCnt; i++){
                Node book = bookList.item(i);
                NamedNodeMap attrs = book.getAttributes();
                for(int j=0; j<attrs.getLength(); j++){
                    Node attr = attrs.item(j);
                    System.err.println(attr.getNodeName()+"-"+attr.getNodeValue());//id
                
                }

                NodeList childNodes = book.getChildNodes();
                for(int k=0; k<childNodes.getLength(); k++){
                    if(childNodes.item(k).getNodeType() == Node.ELEMENT_NODE){            
                        System.out.println(childNodes.item(k).getNodeName()+"-"+ childNodes.item(k).getTextContent()); }}}}catch (ParserConfigurationException e) {           
            e.printStackTrace();
        } catch (SAXException e) {            
            e.printStackTrace();
        } catch(IOException e) { e.printStackTrace(); }}}Copy the code

SAX (Simple API for XML) parsing

advantages

  1. Parsing can begin without waiting for all the data to be processed
  2. Data is checked only as it is read and does not need to be kept in memory
  3. Parsing can be stopped when a certain condition is met, rather than parsing the entire document
  4. High efficiency and performance, can parse more than the system memory documents

disadvantages

  1. Parsing logic is complex, the application layer is responsible for logic processing, the more complex the document, the more complex the program
  2. One-way navigation, unable to locate document layers, difficult to access different parts of the same document at the same time, no support for XPath

Analytical steps

  1. Get an instance of a SAXParserFactory
  2. throughfactory()Get a SAXParser instance
  3. To create ahandler()object
  4. By the parserparse()Method to parse the XML

SAXTest.java

package Paint;

import java.io.IOException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.SAXException;

public class SAXTest {

    public static void main(String[] args) {
        // Get the instance
        SAXParserFactory factory = SAXParserFactory.newInstance();
        try {
            SAXParser parser = factory.newSAXParser();
            SAXParserHandler handler = new SAXParserHandler();
            parser.parse("./src/Paint/hello.xml", handler);

            System.err.println("There are"+ handler.getBookList().size()+ "Book");
            for(Book book : handler.getBookList()){             
                System.out.println(book.getName());
                System.out.println("id="+ book.getId()); System.out.println(book.getAuthor()); System.out.println(book.getYear()); System.out.println(book.getPrice()); System.out.println(book.getLanguage()); }}catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch(IOException e) { e.printStackTrace(); }}}Copy the code

SAXParserHandler.java

package Paint;

import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class SAXParserHandler extends DefaultHandler {
    String value = null;
    Book book = null;
    private ArrayList<Book> bookList = new ArrayList<Book>();

    public ArrayList<Book> getBookList(a) {
        return bookList;
    }
    /* * Start XML parsing */
    public void startDocument(a) throws SAXException {
        super.startDocument();
        System.out.println("XML parsing begins");
    }

    /* XML parsing ends */
    public void endDocument(a) throws SAXException {  
        super.endDocument();
        System.out.println("End of XML parsing");
    }

    /* * Start parsing XML elements */
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
      
        super.startElement(uri, localName, qName, attributes);

        if(qName.equals("book")){
            book = new Book();

            for(int i=0; i<attributes.getLength(); i++){ System.out.println(attributes.getQName(i)+"-"+attributes.getValue(i));
                if(attributes.getQName(i).equals("id")){ book.setId(attributes.getValue(i)); }}}else if(! qName.equals("bookstore")){
            System.out.print("Node name:"+ qName + "-"); }}/* * End of parsing XML element */
    public void endElement(String uri, String localName, String qName)
            throws SAXException {
     
        super.endElement(uri, localName, qName);
        if(qName.equals("book")){
            bookList.add(book);
            book = null;
        }
        else if(qName.equals("name")){          
            book.setName(value);
        }else if(qName.equals("year")){
            book.setYear(value);
        }else if(qName.equals("author")){
            book.setAuthor(value);
        }else if(qName.equals("price")){
            book.setPrice(value);
        }else if(qName.equals("language")){ book.setLanguage(value); }}public void characters(char[] ch, int start, int length)
            throws SAXException {
        super.characters(ch, start, length);

        // Get the array of node values
        value = new String(ch, start, length);
        if(! value.trim().equals("")){
            System.out.println("Node value:"+value); }}}Copy the code

JDOM parsing

You need to introduce jdom.jar

Characteristics of the

  1. Use only concrete classes, not interfaces
  2. The Collections class makes heavy use of the API

Analytical steps

  1. Create a SAXBuilder object
  2. Create an input stream and load the XML file into the input stream
  3. Through saxBuilderbuild()Method to load the input stream into the saxBuilder
  4. Get the root node of the XML file through the Document object
  5. Gets the List collection of child nodes under the root node
package Paint;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

public class JDOMTest {
    private static ArrayList<Book> booksList = new ArrayList<Book>();
   
    public static void main(String[] args) {
        SAXBuilder saxBuilder = new SAXBuilder();
        InputStream in;
        try {
            in = new FileInputStream("./src/Paint/hello.xml");
            InputStreamReader isr = new InputStreamReader(in, "UTF-8");
           
            Document document = saxBuilder.build(isr);         
            Element rootElement = document.getRootElement();           
            List<Element> bookList = rootElement.getChildren();
          
            for (Element book : bookList) {
                Book bookEntity = new Book();
               
                List<Attribute> attrList = book.getAttributes();
               
                for (Attribute attr : attrList) {                   
                    String attrName = attr.getName();            
                    String attrValue = attr.getValue();
                    System.out.println( attrName + "--" + attrValue);
                    if (attrName.equals("id")) { bookEntity.setId(attrValue); }}// Iterates over the node names and values of the children of the book node
                List<Element> bookChilds = book.getChildren();
                for (Element child : bookChilds) {
                    System.out.println(child.getName() + "--"+ child.getValue());
                    if (child.getName().equals("name")) {
                        bookEntity.setName(child.getValue());
                    }
                    else if (child.getName().equals("author")) {
                        bookEntity.setAuthor(child.getValue());
                    }
                    else if (child.getName().equals("year")) {
                        bookEntity.setYear(child.getValue());
                    }
                    else if (child.getName().equals("price")) {
                        bookEntity.setPrice(child.getValue());
                    }
                    else if (child.getName().equals("language")) {
                        bookEntity.setLanguage(child.getValue());
                    }
                }
               
                booksList.add(bookEntity);
                bookEntity = null;
                System.out.println(booksList.size());
                System.out.println(booksList.get(0).getId());
                System.out.println(booksList.get(0).getName()); }}catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (JDOMException e) {
            e.printStackTrace();
        } catch(IOException e) { e.printStackTrace(); }}}Copy the code

DOM4J (Document Object Model for Java) parsing

Dom4j-1.6.1.jar needs to be introduced

advantages

  1. Performance is very good
  2. The extensive use of Java collection classes is easy to develop and provides some alternative ways to improve performance
  3. Support XPath

disadvantages

  1. The API is complicated

steps

  1. Create a reader object for SAXReader
  2. Through the reader objectread()Method to load the books.xml file and get the Document object
  3. Obtains the root node bookstore through the Document object
  4. Through the Element objectelementIterator()Get iterator
  5. Iterators are traversed to get the information in the root node
  6. Gets the property name and property value for book
  7. Through the book objectelementIterator()Gets an iterator for a node element
  8. Iterators are traversed to get the information in the child nodes
  9. Get the node name and value
package Paint;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
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 DOM4JTest {
    
    public static void main(String[] args) {
        ArrayList<Book> bookList = new ArrayList<Book>();
        SAXReader reader = new SAXReader();
        try {            
            Document document = reader.read(new File("./src/Paint/hello.xml"));
            Element bookStore = document.getRootElement();
            Iterator it = bookStore.elementIterator();
            while (it.hasNext()) {            

                Element book = (Element) it.next();
                Book bookData = new Book();             
                List<Attribute> bookAttrs = book.attributes();
                for (Attribute attr : bookAttrs) {
                    System.out.println(attr.getName() + "-" + attr.getValue());

                    if(attr.getName().equals("id")){
                        bookData.setId(attr.getValue());
                    }
                }               
                Iterator itt = book.elementIterator();

                while (itt.hasNext()) {
                    Element bookChild = (Element) itt.next();
                   
                    System.out.println(bookChild.getName()+ "-" + bookChild.getText());

                    if(bookChild.getName().equals("name")){
                        bookData.setName(bookChild.getText());
                    }else if(bookChild.getName().equals("author")){
                        bookData.setAuthor(bookChild.getText());
                    }else if(bookChild.getName().equals("year")){
                        bookData.setYear(bookChild.getText());
                    }else if(bookChild.getName().equals("price")){
                        bookData.setPrice(bookChild.getText());
                    }else if(bookChild.getName().equals("language")){ bookData.setLanguage(bookChild.getText()); }}// After a node is traversed, the node information is added to the listbookList.add(bookData); }}catch (DocumentException e) {

            e.printStackTrace();
        }
        
        // Output XML information stored in memory
        for(Book book:bookList){
            System.out.println(book.getName());
            System.out.println("id="+ book.getId()); System.out.println(book.getAuthor()); System.out.println(book.getYear()); System.out.println(book.getPrice()); System.out.println(book.getLanguage()); }}}Copy the code

🔗www.jianshu.com/p/9b3835299…