1. Dom parsing Dom is an application interface (API) of HTML and XML. It organizes nodes and information fragments in a hierarchical structure (similar to a tree), maps the structure of XML documents, and allows users to obtain
And any part of the operation documentation, is the W3C official standard
[Advantages]
① Allow the application to make changes to the data and structure.
② Access is bidirectional. You can navigate up and down the tree at any time to obtain and operate any part of data.
[Disadvantages]
① It is usually necessary to load the entire XML document to construct a hierarchy, which consumes a lot of resources.
[Explanation]
① Construct a Document Object:
Documentbuilderfactory DBF = documentbuilderfactory. newinstance ();
Documentbuilder DB = BDF. newdocumentbuilder ();
Inputstream is = thread. currentthread (). getcontextclassloader (). getresourceasstream (XML file );
Document Doc = BD. parse (is );
② Traverse DOM objects
Document: XML Document Object, obtained by the parser
Nodelist: node Array
Node: node (including element and # text)
Element: an element that can be used to obtain attribute parameters.
2. Sax (Simple API for XML) parses the "push" Model Analysis Method in the stream model. Event-driven, an event is triggered every time a node is found. The event is pushed to the event processor through the callback method.
Complete parsing. The logic for parsing XML documents must be completed by the application.
[Advantages]
① The analysis can start immediately without waiting for all data to be processed.
② Check the data only when reading the data and do not need to be stored in the memory.
③ You can stop parsing when a condition is met without parsing the entire document.
④ High efficiency and performance, able to parse documents larger than the system memory.
[Disadvantages]
① The application must be responsible for the tag processing logic (for example, maintaining the parent/child relationship). The more complicated the document, the more complicated the program will be.
② One-way navigation, unable to locate the document hierarchy, it is difficult to access different parts of the same document at the same time, does not support XPath.
Principle]
In short, the document is scanned sequentially. When the document starts and ends, and the element starts and ends, the event is notified.
Process the processing function (callback function) until the end of the document.
[Event Processor type]
① Access xml dtd: dtdhandler
② Low-level access parsing error: errorhandler
③ Access document content: contexthandler
[Defaulthandler class]
The default base class of the sax event handler, which implements the dtdhandler, errorhandler, contexthandler, and entityresolver interfaces.
The method is to inherit the base class and override the required methods, such as startdocument ()
[Create a SAX Parser]
Saxparserfactory saxf = saxparserfactory. newinstance ();
Saxparser sax = saxf. newsaxparser ();
Note: For Traversal
① Depthi-first traserval)
② Width-first traserval)
3. JDOM (Java-based Document Object Model) Java-specific Document Object Model. It does not contain a parser.
[Advantages]
① Using specific classes instead of interfaces simplifies Dom APIs.
② A large number of Java Collection classes are used to facilitate Java developers.
[Disadvantages]
① There is no good flexibility.
② Poor performance.
4. dom4j (Document Object Model for Java) is easy to use. It adopts a Java Collection framework and fully supports Dom, sax, and JAXP.
[Advantages]
① A large number of Java Collection classes are used to facilitate Java developers and provide some alternative methods to improve performance.
② Supports XPath.
③ Good performance.
[Disadvantages]
① A large number of interfaces are used, and APIs are more complex.
5. the PULL model analysis method in the Stax (streaming API for XML) stream model. Supports pointer-based and iterator-based methods. jdk1.6 features
[Advantages over push-type resolution]
① In pull parsing, events are generated by the parsing application. Therefore, what is provided to the client in pull Parsing is a parsing rule, not a parser.
② Compared with the push-type parsing, the pull-type parsing code is simpler, and there is no need for so many libraries.
③ The pull parsing client can read multiple XML files at a time.
④ Pull parsing allows you to filter XML files and skip parsing events.
[Overview]
The implementation of Stax API is to use Java Web Service Development (jwsdp) 1.6, combined with the Sun Java streaming XML analyzer (sjsxp)-it is located in
In the javax. xml. Stream package. The xmlstreamreader interface is used to analyze an XML document, while the xmlstreamwriter interface is used to generate
XML document. Xmleventreader is responsible for analyzing XML events using an object event iteration sub-analysis-the cursor mechanism used with xmlstreamreader
Contrast.