Go back to list of previously asked questions and answers
Question: |
Q: What is the difference between DOM-based parser and SAX-based parser? |
Asked By: |
Sharmila |
Viewed: |
1754 |
Answer: |
Document Object Model (DOM) based parser loads the entire document in memory, and allows random access to the document, and it's very easy to update/delete node values.
Simple API for XML (SAX) based parser reads the XML documents as an input stream; it does not load the entire document into the memory. SAX based parser reads the input XML document character by character and notifies the application of various events, such as start of the document, start of an element, character data, end of an element, and so on. Application can then handle these events and process the data.
The XML processing using DOM is preferred when you want to access data randomly; or when you want to update the document. The DOM programming model is much simpler than the SAX, because while using SAX parser, the application is required to implement certain interfaces in order to handle the events and the application also needs to maintain the state. However, for large XML document, SAX is a better option, as it does not load the input XML document into memory, hence SAX requires less system resources and with SAX you don't have to wait till the document is fully loaded into the memory; the SAX based parser will start reading the document, generating the events, without caching any data in memory / without creating an in-memory representation of the document (as in DOM).
In summary, DOM parser generates an in-memory representation of the input XML document by loading the entire document in memory before you can process it, it has a much simpler programming model and is well suited when random document access or document updates is a requirement.
On the other hand, SAX parser is a forward-only stream based, event-based parser, well suited for large documents, or when no random-access or document-updates are desired.
Check out PerfectXML DOM/SAX focus section for some more links.
|
Go back to list of previously asked questions and answers
|