Difference between SAX Parser and DOM Parser in Java


Both SAX and DOM are a type of XML parser APIs. Here, API stands for Application Programming Interface and Parser is used to read and extract content from an XML document in desired format. From this line, it is clear that SAX and DOM are used to read XML documents.

APIs are a modern way to migrate real time information on the Web. In this article, we will discuss the difference between SAX and DOM Parser in Java.

Type of XML Parser

Before going further in this article, let’s briefly discuss XML and its type.

XML

Its full form is eXtensible Markup Language and it is said to be a data description language. In it, the user can define its own tag depending on the need. It stores information in tree based structure that makes it simple and easily understandable.

This is the sample XML document

<?xml version="1.0"?>
<grocery>
   <cart id = "c101">
      <item> Milk </item>
      <price> 65 </price>
   <quantity> 15 </quantity>
   </cart>
   <cart id = "c102">
      <item> Bread </item>
      <price> 30 </price>
      <quantity> 10 </quantity>
   </cart>
   <cart id = "c103">
      <item> Butter </item>
      <price> 40 </price>
      <quantity> 5 </quantity>
   </cart>
</grocery>

Transferring data from one source to another source requires a transformation of the data format. By parsing methods like DOM and SAX, we can read and transform XML data into the required format.

SAX Parser

It is an abbreviation of Simple API for XML. It reads the XML document line by line from start to finish. Whenever it encounters any tag during parsing, it calls the method and retrieves the information for the user.

For example, suppose we want to access the address from an XML document and there is a tag name ‘address’ in that document. In that case, when SAX parser reached that tag, it will call the method to retrieve the address.

Interfaces of SAX Parser

  • SAXParserFactory − It is the object of Parser, it is the first task of parsing

  • SAXParser − It defines a method named ‘parse()’ that is used for parsing.

  • SAXReader − It handles communication with SAX event handlers.

DOM Parser

It is an abbreviation of the Document Object Model and is developed by the World Wide Consortium(W3C). First, it reads the whole XML document and converts the entire document into the form of a tree to store in memory. This tree has a single root node and multiple parent and child nodes. The below figure depicts a tree of the above XML document−

In the above figure, ‘grocery’ is the root node. The ‘cart’ is the parent node and the remaining nodes are its child.

We create an instance named DocumentBuilder of DocumentBuilderFactory Interface which has a built-in parse method that takes an XML document as an argument and converts it into a DOM Tree.

Difference between SAX and DOM Parser

From the above discussion, we can conclude the following differences between SAX and DOM Parser−

SAX Parser

DOM Parser

It is Simple API for XML documents

It is a Document Object Model.

It was developed by XML-Dev members.

It was developed by the World Wide Consortium(W3C).

SAX works on event based model.

DOM works on tree based model.

It can only perform reading operations on the XML document.

It is bidirectional and can perform both reading and writing operations on the XML document

SAX reads XML files in top down approach and can’t provide random access.

It is better for complex and random access

It is memory efficient and can handle large XML files.

It is not memory efficient and not suitable for handling large files.

It starts processing the given documents from the start and hence, reduces the wait time for parsing

from the start and hence, reduces the wait time for parsing. Before parsing, it creates a DOM Tree. Hence, the application has to wait till the tree gets created.

Conclusion

In this article, we have differentiated SAX from the DOM parser. During this, we discovered XML which is a data description language. It provides various parsers like StAX, DOM and SAX to read and write an XML file. All the parsers are similar in many ways but difference lies in their features and working. Also, they have their own advantages and disadvantages too.

Updated on: 20-Jul-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements