Sax parsing and Dom parsing

Source: Internet
Author: User
Tags xml parser

Sax Parsing Example: http://www.iteye.com/topic/763895

Java Sax parsing is in accordance with the sequence of XML files, step by step to parse, before parsing the XML file, we should first understand the XML file node type, one is Elementnode, one is textnode.

to better address the problem of large XML processing, Java developers invented SAX. SAX handles XML in an event-driven manner by defining a callback method for each element, attribute, and content (all of which are considered events), which is provided by the application. The parser reads the XML as a data stream and invokes the appropriate callback method when it encounters an element, attribute, or content. The advantage of SAX is its high processing efficiency and its suitability for processing large XML. The disadvantage is that sax is read-only to XML, is not capable of writing to XML, and that sax does not have the DOM handy when dealing with elements in the XML that are related to each other, because the application must retain state information for the previous event itself. Only the required file contents are read sequentially, and are not read all at once, and are not limited by file size.

But it is difficult to code with a SAX parser, and it is difficult to access multiple different data in the same document at the same time.

Dom Parsing example: http://www.iteye.com/topic/763926

Dom parsing is to load an XML file, assemble it into a DOM tree, and parse the XML file through the relationships between the nodes and the nodes.

the DOM's approach to XML is to read the entire XML into memory to form a tree structure, and then traverse, insert, delete, prune, and manipulate the number of methods. Because DOM is the official standard of the entire language, all languages have parsers that support DOM, including Java, C + +, Perl, JavaScript, and so on. The advantage of DOM is that it is informative (all in memory) and can be accessed at random, especially when dealing with interrelated elements before and after processing. The disadvantage of DOM is that XML applications must first read the entire XML into memory and generate a tree structure by the XML parser before processing the XML, and if the XML is very large, such as 10M, the parsing process is very slow. An equally resource-intensive operation such as an XSLT transformation, which is an operation that must use the DOM, can deplete the system's memory resources. So the standard DOM is only suitable for small and medium-sized XML processing.
The DOM is a collection of nodes or pieces of information that are organized in a hierarchical structure. This hierarchy allows developers to look for specific information in the tree. Analyzing the structure usually requires loading the entire document and constructing the hierarchy before any work can be done. DOM and the generalized tree-based processing have several advantages. First, because the tree is persistent in memory, you can modify it so that the application can make changes to the data and structure. It can also navigate up and down the tree at any time, rather than a one-off process like sax.

SAX: Can only read, can not be modified, only sequential access, suitable for large-scale XML parsing, parsing speed!
     dom: Not only can read, but also can modify, And be able to achieve random access, the disadvantage is that the parsing speed is slow, only suitable for small documents parsing
                           data access to achieve cross-platform! )

DOM: General application with small configuration XML, convenient for us to operate!

Sax is a lightweight approach compared to DOM. We know that when we work with the DOM, we need to read through the entire XML document and then create the DOM tree in memory, generating each node object on the DOM tree. This is not a problem when the document is small, but once the document is large, processing the DOM becomes quite time-consuming and laborious. In particular, the need for memory will grow exponentially, so that using the DOM in some applications is not a cost-effective thing (such as in applets). At this point, a good alternative to the solution is sax.

SAX is conceptually completely different from DOM. First, unlike Dom's document-driven, it is event-driven , that is, it does not need to read the entire document, and the document's read-in process is the SAX parsing process. The so-called event-driven, refers to a method based on the callback (callback) mechanism of program operation. The XmlReader accepts an XML document and parses it as it is read into the XML document, meaning that the process of reading the document and parsing is done at the same time, which differs greatly from the DOM. Before parsing begins, you need to register a contenthandler with XmlReader, which is the equivalent of an event listener, which defines a number of methods in ContentHandler, such as Startdocument (), which is customized when the parsing process The things that should be handled when the document starts. When XmlReader reads the appropriate content, it throws the corresponding event and proxies the event's processing rights to ContentHandler, calling its corresponding method to respond.

If you want to combine DOM parsing with sax parsing, you can use Jdom .

Here are two articles about SAX2:

Http://www.blogjava.net/junhong/archive/2006/11/24/83188.html

http://wtnzuodan.blog.163.com/blog/static/955283002008111792141674/

There is another article did not read:

http://blog.csdn.net/zgjxwl/article/details/9380079

Sax parsing and Dom parsing

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.