Difference between DOM and SAX Parser

The difference between DOM Vs SAX Parser is a very popular java interview question and is often asked when interviewed on Java and XML. Both DOM and SAX parsers are extensively used to read and parse XML files in java applications and both of them have their own set of advantages and disadvantages.

In this post, we are listing down some big and easily seen differences between both parsers.

1. DOM Parser

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 the client. A DOM parser always serves the entire document no matter how much is actually needed by the client.
  • With the DOM parser, method calls in client application have to be explicit and form a kind of chained method calls.

2. SAX Parser

SAX parser is an 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 an 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 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 Parsers

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

SAX

  • Parses until you tell it to stop
  • Fires event handlers for each:
    1. Start tag
    2. Tag body
    3. 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 longer to learn how to work with it.

A SAX Parser, however, is much more space efficient in the 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 fewer functions which means 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 !!

Comments

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.