How DOM, SAX, JDOM, dom4j, and pull work in parsing XML files and compare their pros and cons

Source: Internet
Author: User

Turn from: http://blog.csdn.net/seu_calvin/article/details/52027484

1. DOM (document object model)
(1) Dom is a set of specifications specified by the consortium, the core of the DOM is to process the data by the tree structure, the DOM parser reads the XML file and constructs an identical tree in memory, can specify the element to be accessed, random access, modify the XML file arbitrarily. In particular, it is very easy to process forward.

(2) DOM is based on memory, regardless of the size of the file, will be all the content preloaded into memory. Which consumes a lot of memory space.

2. SAX (Simple API for XML) is used for XML

(1) Sax is based on event-driven, reading XML files in a stream-like manner, meaning that the process of reading the document and parsing the process are simultaneous. Therefore, you don't even have to parse the entire document and stop parsing when a condition is met, so it takes up less memory.

The SAX parser employs an event-based model that triggers a series of events when parsing an XML document, and when the specified tag is found, activates a callback method that tells the method that the specified tag has been found. When an event is triggered, a partial data of the corresponding XML is obtained.

(2) Sax can only read XML and cannot insert data into the file.

(3) Sax can not be returned to the resolved part of the process. As a result, sax lacks flexibility compared to DOM parsing.

3. JDOM

Dom&sax is the underlying interface for parsing XML. Jdom and DOM4J are more advanced encapsulation classes based on the underlying APIs. (Dom is generic, while Jdom and dom4j are oriented to the Java language)

(1) Jdom is a tree based, processing XML Java API that loads trees into memory and is therefore not suitable for processing large documents. But there are two main differences with DOM.

The first jdom is to use a specific class without using an interface. This simplifies the API in some ways, but it also limits flexibility. Second, the API uses a lot of Java collection classes to make it easier for Java developers to use.

(2) The jdom itself does not contain a parser. It typically uses the SAX2 parser to parse and validate the input XML document. It contains a number of converters that represent Jdom as SAX2 event streams, DOM models, or XML text documents.

4. dom4j

Dom4j is a very, very good Java XML API with excellent performance, powerful features and easy to use. It is particularly worth mentioning that the famous hibernate also uses dom4j to read XML configuration files.

(1) DOM4J uses interfaces extensively and has more complex APIs, so dom4j has more flexibility than jdom. Java collection classes are used extensively to facilitate Java developers.

(2) The disadvantage is that the API is too complex.

5. PULL

Pull technology has been integrated into the Android system, so there is no need to introduce additional jars when using pull.

Pull provides a sax-like event-handling mechanism, but the main difference is that the SAX parser works by automatically pushing events into the registered event handler, and the pull parser allows your code to actively retrieve events from the parser. Therefore, you can no longer get the event and end the resolution if the required conditions are met.

The pull-related package in the Android system is ORG.XMLPULL.V1, which provides the pull parser's factory class Xmlpullparserfactory and pull parser Xmlpullparser, the former instance invocation Newpullparser method creation The latter instance, then the latter instance can call Geteventtype () and next () and so on to actively extract the event, and according to the extracted event type corresponding logical processing.


6. Summary

(1) If the XML document is large and does not consider the portability problem, it is recommended to adopt dom4j;

(2) If the XML document is small, Jdom is recommended, and sax is considered if you need to process it in time without saving data.

(3) Android with pull more, after all, is the system integration.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.