HomeXML Page 8 - XML Parsing With DOM and Xerces (part 2)
Black Or White - XML
In this concluding article of a two-part series, use your knowledge of DOM processing with Xerces to construct simple Web applications based on Xerces, XML and JSP. Examples include transforming an XML file to HTML via Xerces and dynamically generating an XML document tree from a MySQL database.
Now, you can parse a document using either DOM or SAX, and achieve the same result. But which is better?
As with all complex questions, there's no simple answer.
The SAX approach is event-centric - as the parser travels through the document, it executes specific functions depending on what it finds. Additionally, the SAX approach is sequential - tags are parsed one after the other, in the sequence in which they appear. Both these features add to the speed of the parser; however, they also limit its flexibility in quickly accessing any node of the DOM tree.
As opposed to this, the DOM approach builds a complete tree of the document in memory, making it possible to easily move from one node to another (in a non-sequential manner). Since the parser has the additional overhead of maintaining the tree structure in memory, speed is an issue here; however, navigation between the various "branches" of the tree is easier. Since the approach is not dependent on events, developers need to use the exposed methods and attributes of the various DOM objects to process the XML data.
The DOM parser is a little slower, since it has to build a complete tree of the XML data, whereas the SAX parser is faster, since it's calling a function each time it encounters a specific tag type. You should experiment with both methods to see which one works better for you.