posted 23 years ago
SAX is based on 'events' ( aka call-backs ). It is simple, sequential way of looking at the XML data. Advantages of using SAX are it is lightweight in resource consumption and it is quite fast too. The downside of it is that it is a 'read only' mechanism ie., you will not be able to modify the data. Since it is sequential, you will not have access to past elements or peek ahead and see what is next. No random access either.
DOM on the otherhand reads the entire XML document once and creates a 'tree-like' structure in the memory. You can seek 'nodes' back and forth and traverse the XML tree in any random order. Unlike SAX you can modify the XML data and save it back which makes it a better choice when you need to change the XML data during parsing. You can also peek ahead or go back to any node. The downside of DOM is that since the entire XML is stored in the memory and manipulated, it is memory intensive.
Whether to use SAX or DOM is more of a design choice and should be driven by what you want to achieve. For example, if you need flexibility of being able to manipulate different elements and traverse different nodes in a non-sequential manner, DOM is the obvious choice. But DOM can be a pain if your XML file is large. I have seen some implementations where SAX is used with some additional programming to achive the flexibility of DOM while not compromising on performance.
Hope that helps!
Ajith
Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).