The difference between sax/dom/jdom/dom4j

Source: Internet
Author: User

Online can find a lot of 4 ways to resolve the difference, I will do an excerpt and summary, incidentally, to make a backup.


Sax

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

Because the event trigger is sequential, the SAX parser provides a sequential access mechanism to the XML document, and for the part that has already been parsed,

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

When implemented, the SAX parser simply checks the stream of bytes in the XML document sequentially, judging which part of the XML syntax the current byte is, and checking to see if it conforms to the

XML syntax and triggers the corresponding event. For the event handler itself, it is implemented by the application itself. The SAX parser uses an event-based model that can trigger a sequence 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 alter the application to access only the data in the XML document

, the SAX parser is highly efficient.

Sax is also less memory-demanding.

Pros: 1. No need to load the entire document into memory, so memory consumption is low.

2, push model allows to register multiple ContentHandler.

Cons: 1, no built-in document navigation support.

2. XML documents cannot be accessed randomly.

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

4, namespace scope is not supported.

Fit: Read data to an XML document

Dom

The DOM parser transforms an XML document into a tree containing its contents and can traverse the tree. That is, through the DOM tree, the application can randomly access the XML document. This access provides a great amount of flexibility in the development of the application, which can arbitrarily control the contents of the entire XML document. Programming with DOM parsing is easy, developers just need to invoke build instructions, and then use navigation APIs to access the desired tree nodes to complete the task. It is easy to add and modify elements in the tree. However, because of the need to process the entire XML document when using the DOM parser, the performance and memory requirements are high, especially when encountering large XML files. 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, can be easily navigated.

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

Cons: 1. The entire XML document must be parsed at once.

2. The entire DOM tree is loaded into memory with high memory requirements.

3. A generic DOM node is not ideal for object type bindings that must create objects for all nodes.

Apply: Modify data on XML document

Jdom

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

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

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

2, there is no backward compatibility limit, so it is simpler than DOM.

3, the speed is fast.

4. Java rules with Sax.

Cons: 1, cannot handle documents larger than memory.

2. jdom represents the XML document logical model and does not guarantee that each byte is truly transformed.

3. For instance documents, no actual model of DTD and schema is provided.

4. It is not supported to traverse packages in the DOM appropriately.

Apply: Choose by yourself

dom4j

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

Advantages: 1. Maximum flexibility

2, easy to use and powerful, excellent performance

Cons: 1, Complex API

2. Poor transplant

Apply: Choose by yourself


The difference between sax/dom/jdom/dom4j

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.