cancel
Showing results for 
Search instead for 
Did you mean: 

SAX parser

Former Member
0 Kudos

What are the advantages of SAX over DOM?

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Answers (2)

Answers (2)

Former Member
0 Kudos

Hi Sankar Choudhury ,

Both DOM and SAX approaches have advantages and disadvantages, and your choice of technique must depend on the type of data being parsed, the requirements of your application, and the constraints under which you are operating.

The SAX approach is linear: It processes XML structures as it finds them, generating events and leaving the event handlers to decide what to do with each structure. The advantage of this approach is that it is resource-friendly; because SAX does not build a tree representation of the document in memory, it can parse XML data in chunks, processing large amounts of data with very little impact on memory. This also translates into better performance; if your document structure is simple, and the event handlers don't have anything too complicated to do, SAX-based applications will generally offer a speed advantage over DOM-based ones.

The downside, though, is an inability to move around the document in a non- linear manner. SAX does not maintain any internal record of the relationships between the different nodes of an XML document (as the DOM does), making it difficult to create customized node collections or to traverse the document in a non-sequential manner. The only way around this with SAX is to create your own custom object model, and map the document elements into your own custom structures—a process that adds to complexity and can possibly degrade performance.

Where SAX flounders, though, the DOM shines. The DOM creates a tree representation of the document in memory, making it possible to easily travel from one node to another, or even access the same node repeatedly (again, not something you can do easily in SAX). This tree representation is based on a standard, easy-to-understand model, making it easier to write code to interact with it.

This flexibility does, however, come with an important caveat. Because the DOM builds a tree in memory, DOM processing cannot begin until the document has been fully parsed (SAX, on the other hand, can begin parsing a document even if it's not all available immediately). This reduces a developer's ability to "manage" the parsing process by feeding data to the parser in chunks, and also implies greater memory consumption and consequent performance degradation.

Consequently, the choice of technique depends significantly on the type of constraints the application will be performing under, and the type of processing it will be expected to carry out. For systems with limited memory, SAX is a far more efficient approach. On the other hand, complex data-processing requirements can benefit from the standard object model and API of the DOM.

After using DOM to parse XML documents for any length of time, you will probably begin to notice that performance tends to suffer when you’re dealing with large documents. This problem is endemic to DOM's tree-based structure: larger trees demand more memory, and DOM must load an entire document into memory before it can allow you to parse it. For situations where performance is problematic with DOM, you have an alternative in the Simple API for XML (SAX). In this fifth installment in our Remedial XML series, I'll introduce you to the SAX API, and provide some links to SAX implementations in several different languages.

Further differences can be seen in the following websites

http://articles.techrepublic.com.com/5100-22-1044823.html

Parsing XML Efficiently : DOM Parsing ,SAX Parsing

http://www.oracle.com/technology/oramag/oracle/03-sep/o53devxml.html

SAX Example

http://www.informit.com/articles/article.aspx?p=26351&seqNum=6&rl=1

Event or DOM parsing?

http://discuss.joelonsoftware.com/default.asp?design.4.156750.12

cheers!

gyanaraj

****Pls reward points if u find this helpful

prabhu_s2
Active Contributor
0 Kudos