Home > Products > SophiaFramework UNIVERSE > Tutorial > Amazon E-Commerce > - 4 / 9 -

DOM Parser and SAX Parser

To manipulate an XML document, you need an XML parser. SophiaFramework provides you with both DOM parser and SAX parser.

DOM Reviewed

The Document Object Model (DOM) defines a standard way for accessing and manipulating XML documents.

The DOM presents an XML document as a tree-structure (a node tree), with the elements, attributes, and text defined as nodes.

With the Document Object Model, programmers can build documents, navigate their structure, and add, modify, or delete elements and content.

SAX Reviewed

The Simple API for XML (SAX) provides a mechanism for parsing XML files in the event-driven style. A parser which implements SAX must define a number of methods that will be called when events occur during parsing.

With SAX, the previously parsed data cannot be re-read without starting the parsing operation again.

Comparing DOM and SAX

DOM separates the document navigation and data collection by constructing a parsed tree in the memory. Using the tree, one can navigate to any node he is interested in. Then, when a correct node is found, one can collect the data.

SAX does not separate the document navigation and data collection. One has to listen to the events to know where he is in the XML document. In other words, one would need to keep trace of every previously encountered XML tag to see if any matches are made. Interested data can be collected only when the event is in the right place.

Benefits and drawbacks of DOM and SAX

DOM is useful for small documents in which the program needs to process a large portion of the document.

DOM parser can be easier to develop compared with SAX.

SAX is faster and takes much less memory than DOM. (DOM parser must create an entire DOM tree in the memory before any processing can begin, so the amount of memory used by a DOM parser depends entirely on the size of the input data. The memory used by SAX parser is based only on the maximum depth of the XML tree and the maximum data stored in XML attributes on a single XML element.)

SAX is useful for large documents in which the program only needs to process a small portion of the document.

SAX parser can be difficult to develop because of its event-driven nature, especially when the structure of the XML file is complicated.

Go back  1   2   3   4   5   6   7   8   9  Next page