SAX notifies the registered document handler whenever an element beings and ends.
This allows the handle to react to the content a piece at a time. so it can build objects or perform any required actions.
Writing application using SAX can be somewhat tedious however because each event must be caught, identified and processed. This is especially painful when the processing depends on where the element occurs in the three.
DOM builds on top of SAX parser.
DOM builds a tree structure from the document.
Once the entire document has been pared, this tree structure can be navigated and explored at length.
Also, an application can modify the DOM tree and save it as a new XML document.
So, for many application the DOM parser is the clear choice.
The entire document is parsed and held in memory before the tree of elements is returned to the application.
SAX is very efficient and fast because it does no store the XML document in memory. It must fit in a system with memory constraints like hand held devices.
Also for applications that need to perform some actions based on the value of certain elements of XML.
If application needs to keep track of the data and their locations. SAX is not a good fit.
DOM is used when application requires the knowledge of the structure of the original XML document or need not map them to internal data objects.
In the contrast, DOM is very memory intensive. Its wasteful when we need to map data to internal data objects.