Training 27 2018.05.02

XML parsing

  There are three common analysis methods in development, as follows:

    DOM: requires the parser to load the entire XML document into memory and parse it into a Document object.

    SAX: is a faster and more efficient method. It scans the document line by line, parsing it as it scans. And the specific analysis is carried out in an event-driven way. Each line executed will trigger the corresponding event.

    PULL: Android's built-in XML parsing method, similar to SAX.

  The parser is the specific implementation provided according to different parsing methods. Some parsers are too cumbersome to operate. For the convenience of developers, there are parsing development packages that are easy to operate.

  Common parsing development kits:

     JAXP: Sun provides support for DOM and SAX development kits

    JDom: dom4j brother

    jsoup: a development package for handling HTML specific parsing

    dom4j: The more commonly used parsing development kit, which is used at the bottom of hibernate.

  dom4j parsing:

    XML DOM loads the entire XML document into memory, generating a DOM tree,

And get a Document object, through which the DOM can be manipulated. The core concept in the DOM is the node. The elements, attributes, text, etc. in the XML document are all nodes in the DOM.

    Preparation: dom4j.jar is required and imported into the project path.

    dom4j must use the core class SaxReader to load the xml document to obtain the Document to obtain the root element of the document through the Document object, and then it can be operated.

   

      step:

      1. Get the parser

      2. Get the document document object

      3. Get the root element

      4. Get the child elements under the root element

      5. Traverse child elements

      6. Determine the element whose element name is servlet

import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.junit.Test;

public  class TestDom4j {
    @Test
    public  void testReadWebXML() {
         try {
             // 1. Get the parser 
            SAXReader saxReader = new SAXReader();
             // 2. Get the document document object 
            Document doc = saxReader.read("src/com/oracle/demo06/web.xml " );
             // 3. Get the root element 
            Element rootElement = doc.getRootElement();
             // System.out.println(rootElement.getName()); // Get the name of the root element
             // System.out.println(rootElement .attributeValue("version")); // Get the attribute value in the root element
             // 4. Get the child elements under the root element
            List<Element> childElements = rootElement.elements();
             // 5. Traverse child elements 
            for (Element element : childElements) {
                 // 6. Determine the element whose name is servlet 
                if ("servlet" .equals(element.getName( ))) {
                     // 7. Get servlet-name element 
                    Element servletName = element. element("servlet-name" );
                     // 8. Get servlet-class element 
                    Element servletClass = element. element("servlet-class" );
                    System.out.println(servletName.getText());
                    System.out.println(servletClass.getText());
                }
            }

        } catch (DocumentException e) {
            e.printStackTrace ();
        }
    }

}

 

7. Get the servlet-name element

8. Get the servlet-class element

Guess you like

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