The pros and cons of sax, Dom, Jdom, and the differences between file parsing and reading

Source: Internet
Author: User
----The advantages and disadvantages of sax, DOM, and Jdom technology
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 the event trigger itself is sequential, the SAX parser provides a sequential access mechanism to the XML document and cannot be returned to the parsed part.
Sax is called the "simple" application interface because the SAX parser does only a few simple things, and most of the work is done by the application itself. That is, when the SAX parser is implemented, it only checks the byte stream in the XML document sequentially, determining which part of the XML syntax the current bytes are, checking to see if it conforms to the XML syntax and triggering the corresponding event. For the event handler itself, it is implemented by the application itself. Sax parsers do not have the flexibility to handle XML documents compared to DOM parsers, but the SAX parser is more efficient for applications that only need to access data in an XML document without making changes to the document. Because of its simplicity and low memory requirements, the Sax Analyzer has a high efficiency and wide application value.
The DOM parser stores the entire XML document in memory as a DOM tree by analyzing the XML document, and the application can access and manipulate any part of the DOM tree at any time, that is, through the DOM tree, the application can access the XML document randomly. This access gives you a lot of flexibility in the development of your application, which allows you to arbitrarily control the content in the entire XML document. However, because the DOM parser converts the entire XML document into a DOM tree and puts it in memory, the need for memory is higher when the XML document is larger or the document structure is more complex. Also, traversing a tree with a complex structure is a time-consuming operation. Therefore, the DOM Analyzer's requirements for machine performance are relatively high, and the implementation efficiency is not very ideal. However, because the tree structure of the DOM parser is consistent with the structure of the XML document, random access can be achieved through the DOM tree mechanism. Therefore, Dom Analyzer also has a wide range of use value.

Jdom is a pure Java API for processing XML. Using a specific class instead of an interface, it is necessary to generate an instance of most of the node types, as long as one or two of the META elements are passed in. is the outstanding performance of the Java API for processing XML at present. 


Sax

Advantage:

① does not need to load the entire document into memory, thus less memory consumption

② push model allows multiple ContentHandler to be registered

Disadvantage: 

① does not have built-in document navigation support

② is not able to randomly access XML documents
③ does not support modifying XML in situ
④ does not support namespace scopes
Best for: An application that reads data from XML only (not available to manipulate or modify XML documents)


Dom

Advantages: 

① Easy to use

② a rich collection of APIs that can be used to easily navigate
③ the entire tree into memory, allowing random access to XML documents

Disadvantage: 

① the entire XML document must be parsed at once

② loading the entire tree into memory is expensive
③ generic DOM nodes are not ideal for object type bindings that must create objects for all nodes
Best for: Applications or XSLT applications that need to modify XML documents (applications that are not available for read-only XML)


Jdom

Advantage: 

① is a tree-based Java API for processing XML, which loads trees in memory

② has no backward-compatible restrictions and is therefore simpler than DOM
③ fast, less defects
④ Java rules with sax

Disadvantage: 

① cannot handle documents that are larger than memory

②jdom represents the XML document Logical model. There is no guarantee that each byte is truly transformed.
③ does not provide any actual model of DTDs and schemas for instance documents.
④ does not support the corresponding traversal of the package with the DOM
Best for: Jdom with the convenience of trees, as well as the Java rules of Sax. Use when balancing is needed

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.