Difference between DOM Vs SAX Parser is very popular java interview question and often asked when interviewed on Java and XML. Both DOM and SAX parser are extensively used to read and parse XML file in java applications and both of them have their own set of advantages and disadvantages. In this post, I am listing down some big and easily seen differences between both parsers.
1. DOM XML Parser in Java
DOM parser is a tree-based API. A tree-based API is centered around a tree structure and therefore provides interfaces on components of a tree (which is a DOM document) such as Document interface,Node interface, NodeList interface, Element interface, Attr interface and so on.
A DOM parser creates a tree structure in memory from the 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. With DOM parser, method calls in client application have to be explicit and forms a kind of chained method calls.
2. SAX XML Parser in Java
SAX parser is a event-based API. Usually an event-based API provides interfaces on handlers. There are four handler interfaces, ContentHandler interface, DTDHandler interface, EntityResolver interface and ErrorHandler interface.
SAX parser does not create any internal structure. Instead, it takes the occurrences of components of a input document as events, and tells the client what it reads as it reads through the input document. SAX parser serves the client application always only with pieces of the document at any given time. With SAX parser, some custom methods are called [ “callback” methods ] when some certain events occur during parsing on xml document. These methods do not have to be called explicitly by the client, though we could call them explicitly.
3. Difference between DOM and SAX XML Parser in Java
Let’s list down an easy to remember short list of differences.
DOM (Document Object Model)
- Parses entire document
- Represents result as a tree
- Lets you search tree
- Lets you modify tree
- Good for reading data/configuration files
- Parses until you tell it to stop
- Fires event handlers for each:
- Start tag
- Tag body
- End tag
- Low level APIs
- Good for very large documents, especially if you only care about very small portions of the document.
4. How to choose between DOM and SAX Parsers?
Ideally a good parser should be fast (time efficient),space efficient, rich in functionality and easy to use . But in reality, none of the main parsers have all these features at the same time. For example, a DOM Parser is rich in functionality (because 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 long to learn how to work with it.
A SAX Parser, however, is much more space efficient in case of 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 less functions which mean that the users themselves have to take care of more, such as creating their own data structures.
I think the answer really depends on the characteristics of your application and your current requirements.
5. Can SAX and DOM parsers be used at the same time?
Yes, of course, because the use of a DOM parser and a SAX parser is independent. For example, if your application needs to work on two XML documents, and does different things on each document, you could use a DOM parser on one document and a SAX parser on another, and then combine the results or make the processing cooperate with each other.
Happy Learning !!