A SAX (Simple API for XML) parser does not create any internal structure. Instead, it takes the occurrences of components of an input document as events (i.e., event driven), and tells the client what it reads as it reads through the input document.
A SAX parser serves the client application always only with pieces of the document at any given time.
A SAX parser, however, is much more space efficient in case of a big input document (because it creates no internal structure). What’s more, it runs faster and is easier to learn than DOM parser because its API is really simple. But from the functionality point of view, it provides a fewer functions, which means that the users themselves have to take care of more, such as creating their own data structures.
Use SAX parser when
Input document is too big for available memory.
When only a part of the document is to be read and we create the data structures of our own.
If you use SAX, you are using much less memory and performing much less dynamic memory allocation.
SAX Parser example: Xerces, Crimson etc Use JAXP (Java API for XML Parsing) which enables applications to parse and transform XML documents independent of the particular XML parser. Code can be developed with one SAX parser in mind and later on can be changed to another SAX parser without changing the application code.
A DOM (Document Object Model) parser creates a tree structure in memory from an input document and then waits for requests from client.
A DOM parser always serves the client application with the entire document no matter how much is actually needed by the client.
A DOM parser is rich in functionality. It creates a DOM tree in memory and allows you to access any part of the document repeatedly and allows you to modify the DOM tree. But it is space inefficient when the document is huge, and it takes a little bit longer to learn how to work with it.
Use DOM when
Your application has to access various parts of the document and using your own structure is just as complicated as the DOM tree.
Your application has to change the tree very frequently and data has to be stored for a significant amount of time.
DOM Parser example: XercesDOM, SunDOM, OracleDOM etc. Use JAXP (Java API for XML Parsing) which enables applications to parse and transform XML documents independent of the particular XML parser. Code can be developed with one DOM parser in mind and later on can be changed to another DOM parser without changing the application code.