sax/dom/jdom/dom4j the difference? _sax

Source: Internet
Author: User


Sax

When parsing an XML document, the SAX parser triggers a series of events that the application accesses to the XML document through the event handler function.

Because event firings are sequential, the SAX parser provides a sequential access mechanism to XML documents, and for the parts that have been analyzed,

You can't go back and do it again. In addition, it cannot simultaneously access processing 2 tags.

When the SAX parser is implemented, it only checks the byte stream in the XML document sequentially to determine which part of the XML syntax is current, and checks to see if it matches

XML syntax and triggers the corresponding event. For the event handler itself, it is to be implemented by the application itself. The SAX parser employs an event-based model that triggers a series of events when parsing an XML document, and when a given tag is found, it can activate a callback method that tells the method that the label has been found.

The SAX parser lacks flexibility in processing XML documents compared to the DOM parser, but does not change the application for access to only the data in the XML document

, the SAX Analyzer is highly efficient.

Sax is also low on memory requirements.

Advantages: 1, no need to load the entire document into memory, so less memory consumption.

2, the push model allows the registration of multiple ContentHandler.

Disadvantages: 1, there is no built-in document navigation support.

2, can not randomly access the XML document.

3, do not support the modification of XML in situ.

4, namespace scope is not supported.

Fit: Read data to an XML document

Dom

The DOM parser converts an XML document into a tree containing its contents, and can traverse the tree. That is, through a DOM tree, an application can have random access to XML documents. This access gives you a lot of flexibility in the development of your application, and it allows you to arbitrarily control the content in the entire XML document. It is easy to use the DOM parsing model, and developers simply need to invoke the build instructions and then use the navigation APIs to access the required tree nodes to complete the task. You can easily add and modify elements in the tree. However, because of the need to process the entire XML document with the DOM parser, the performance and memory requirements are high, especially when you encounter a large XML file. Because of its traversal capabilities, DOM parsers are often used in services where XML documents require frequent changes.

Advantages: 1, a rich collection of APIs, you can easily navigate.

2. The entire DOM tree is loaded into memory, allowing random access.

Disadvantages: 1, the entire XML document must be parsed at once.

2, the entire DOM tree is loaded into memory, high memory requirements.

3. General DOM nodes are not ideal for binding object types that must be created for all nodes.

Apply: Modify data on an XML document

Jdom

Jdom is a pure Java API for processing XML. Use a specific class instead of an interface. Jdom has traversal of the tree and Java rules for Sax. There are two main differences between Jdom and Dom. First, Jdom uses only specific classes instead of interfaces. This simplifies the API in some ways, but it also limits flexibility. Second, the API uses a lot of collections classes, simplifying the use of Java developers who are already familiar with these classes.

The jdom itself does not contain a parser. It typically uses the SAX2 parser to parse and validate the input XML document (although it can also represent the previously constructed DOM as input). It contains converters to output jdom representations as SAX2 event streams, DOM models, or XML text documents. Jdom is the open source that is released under the Apache license variant.

Advantages: 1, is based on the tree-processing XML Java API, the tree loaded into memory.

2, there is no backward-compatible restrictions, so simpler than the DOM.

3, fast.

4. Java rules with Sax.

Disadvantages: 1, unable to process documents larger than memory.

2. Jdom represents the logical model of XML documents and cannot guarantee the true transformation of each byte.

3. No actual model of DTD and schema is provided for instance documents.

4, does not support the corresponding traversal of the package in the DOM.

Apply: Choose by yourself

dom4j

DOM4J has more complex APIs, so dom4j has more flexibility than jdom. DOM4J performance is best, even the Sun's JAXM is also using dom4j. Many open source projects currently use DOM4J, such as the famous hibernate also use dom4j to read XML configuration files. If portability is not considered, then use DOM4J.

Advantages: 1, the highest flexibility

2, ease of use and powerful, excellent performance

Disadvantages: 1. Complex API

2, transplant poor

Apply: Choose by yourself

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.