xml parsing

DOM&SAX parsing principle
Parsing is to read from top to bottom, read one line and parse one line.
    C. The advantage of SAX parsing is that it occupies less memory and has fast parsing speed; the disadvantage is that it is only suitable for reading, and cannot be added, deleted, or modified.
2. DOM parsing
    A. DOM (Document Object Model, or Document Object Model) is the xml parsing method recommended by W3C.
    B. DOM parsing is up to 1. SAX parsing
    A. SAX (Simple API for XML) is not an official standard, but it is the de facto standard in the XML community, and almost all XML parsers support it.
    B. SAX will load the entire XML into the memory to form a Document object, and each node in the xml will also become an object in the memory. According to the relationship between the XML nodes, the relationship between each object will be established in the memory. (Refer to the schematic diagram)
    C, DOM parsing shortcomings, large memory footprint. The advantage is that it is easy to add, delete, modify and check files.



JAXP analysis:

1. JAXP development kit is a part of J2SE, which consists of javax.xml, org.w3c.dom, org.xml.sax package and its sub-packages.

2. Several factory classes are defined in the javax.xml.parsers package. By calling these factory classes, you can get the DOM or sax parser of the xml document, so as to realize the parsing of xml.

3. DocumentBuilderFactory in javax.xml.parsers creates a DOM parser and parses XML; the conversion class Transformer is required to convert the memory Document object into an xml document (refer to the sample code Demo1).

4. sax parsing involves parser and event handler: (refer to the sample code Demo2).

    a. The SAXParserFactory in javax.xml.parsers creates a SAX parser; the content processor needs to be written by the programmer, and the content processor implements the ContentHandler interface.

    b. When the parser parses a component of an XML document through SAX, it will call a method of the event handler, and the parser will pass the currently parsed xml content as a parameter of the method to the event handler.



Guess you like

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