About us
Products
Services
Articles
Contact us

SAX + DOM Mix = SAXDOMIX

Open Source Standards-based Framework for Scalable XML Processing

Download    Benchmark    Browse API

SAX and DOM are the standard APIs for XML parsing and they are supported by JDK 1.4+. Each of those two APIs has PROs and CONs, which are explained next. Why not mixing SAX and DOM to get maximum advantage?

Simple API for XML (SAX) is used to obtain the content of an XML document as a sequence of events. For example, you'll be notified when the parsing starts and ends, when a start / end tag is found or when a chunk of character data is read. This approach is very efficient because the parser doesn't hold the information in memory and the processing is minimal at the parser's level. In most cases, however, you can't do much with the information of a single SAX event. For example, when you get a chunk of character data, you don't know the parent element, unless you've kept in memory the name of the last start tag. You don't even know whether the character data is the entire text of a continuous string or just a fragment, unless you wait for the following events. Therefore, you have to store relevant information in memory for further usage. The good thing is that an application knows exactly what it needs and it also knows when each piece of information isn't necessary anymore. Therefore, SAX lets you build efficient parsing mechanisms, but it is hard-to-use like any low-level API.

Document Object Model (DOM) is used to build in memory a tree structure containing the information of an entire XML document. The DOM API defines interfaces whose instances are linked in a tree and maintain information about elements, attributes, character data sections, processing instructions, etc. DOM isn't the easiest way to manipulate the information of a document, but it has a huge advantage: a DOM tree is a mirror of the original document's structure and content. This makes DOM the ideal foundation for many XML tools such as XPath and XSLT processors. The problems start to occur when you need to process large documents. Some of them will simply not fit into the computer's memory. Therefore, DOM is much easier to use than SAX, but has scalability problems.

Suppose you have a large table stored in an XML format. If you just want to count the records, SAX would do the job. If you must access the records multiple times to sort the table using some algorithm, a DOM tree would be very useful supposing that you have enough memory for the entire table. In many cases, however, you just need to process one record at a time in a loop. It doesn't make sense to read the entire table in a DOM tree, while with SAX you have to code the grouping of the record information into some memory structure. What you really need, in this case, is to get a small DOM sub-tree for each record, process it and then be able to make it available for garbage collection. This can be done by mixing the usage of the standard SAX and DOM APIs.

SAXDOMIX contains classes that can forward SAX events or DOM sub-trees to your application during the parsing of an XML document. The framework defines simple interfaces that allow the application to get DOM sub-trees in the middle of a SAX parsing. After handling, all DOM sub-trees become eligible for garbage collection. This solves the DOM scalability problem. In the above example, you can process a very large table and get each record as a DOM sub-tree.

By mixing SAX and DOM, you can reduce the memory requirements when the application doesn't need the entire DOM tree in memory. In addition, there are pieces of information that can be extracted directly from the SAX events without the need to build a DOM sub-tree. For example, the application could analyze the attributes of an element and decide if it needs the entire DOM sub-tree rooted by that element. Such a technique improves the performance of the application because the creation of many DOM objects takes a lot of CPU-time and memory resources.

Finally, Extensible Stylesheet Language Transformations (XSLT) are the most used way to process XML documents. SAXDOMIX provides special support for this kind of processing. In a mixed transformation, the same XSLT instructions are applied to each DOM sub-tree and the results are inserted into the output document.

Examples


Copyright © 2000-2020 Devsphere

About us
Products
Services
Articles
Contact us