15 June 2006 - 18:49DOM vs SAX
I was in an interview with another BU when the question popped up: "What are the differences between DOM and SAX XML parsers and when would you use one or the other?". I started with a brief description of DOM (tree-like representation of an XML document that loads an entire document upfront) and SAX (slim, event-based XML parser). The answer that I gave was that I would choose DOM for XML documents that are complex and prone to change while I would favor SAX for simple documents which are not prone to change (memory was not an issue). The reason that I gave this answer was that trying to keep track of where you are in a complex document while SAX events are firing is pretty complicated and that once you created a SAX event handler that maps onto a document format is pretty hard to change it in order to map it onto a new format. The team interviewing me agreed and passed to another question.
Now that I’m thinking about it, the answer was wrong. The right answer would have been that I would choose an object-to-XML mapper that would translate a XML document into objects and viceversa and that I would delegate all processing and resource management to the OX mapper. The days when we would have to deal with DOM or SAX are over for the most of us, we should be working with what are comfortable with, that is with objects and not DOM Nodes or SAX events.
Later edit: SAX is a parser really close to the metal begging to have some extension layered on top of it in order to make it more developer-friendly. The fact that no such extension exists or is not popular (I, for one, do not know of any) leads me to believe that SAX is not so widely used. Probably the reasons for which it was conceived (small memory footprint being the most important one) are not important anymore or the XML documents that are usually manipulated do not require a lot of resources.
Even later edit: It looks like the size of the XML documents and the number of formats grow pretty rapidly as SOA matures. A SAX-based OX mapper would not be a bad idea after all…
No Comments | Tags: Development, Favorites