top of page

DOM vs SAX Parser in Java - XML Parsing in Java



DOM vs SAX parser in Java

DOM and SAX parser are two most popular parser used in Java programming language to parse XML documents. DOM and SAX concept are originally XML concept and Java programming language just provide an API to implement these parser. Despite both DOM and SAX are used in XML parsing, they are completely different to each other. In fact difference between DOM and SAX parser is a popular Java interview question asked during Java and XML interviews. DOM and SAX parser has different way of working, which makes Java programmer to understand difference between DOM and SAX parser even more important.


Careless use of DOM parser may result in java.lang.OutOfMemoryError if you try to parse a huge XML file and with small heap size while careless use of SAX parser may result in poor performance while parsing small and medium sized XML files with good enough heap space in Java. In this Java article we will compare DOM and SAX parser and learn difference between them.



SAX vs DOM parser - Java

In this section, we will see some behavioral difference between DOM and SAX parser. These difference will help you to choose DOM over SAX or vice-versa based upon size of XML files and availability of heap memory in JVM.

  1. First and major difference between DOM vs SAX parser is how they work. DOM parser load full XML file in memory and creates a tree representation of XML document, while SAX is an event based XML parser and doesn't load whole XML document into memory.

  2. For small and medium sized XML documents DOM is much faster than SAX because of in memory operation.

  3. DOM stands for Document Object Model while SAX stands for Simple API for XML parsing.

  4. Another difference between DOM vs SAX is that, learning where to use DOM parser and where to use SAX parser. DOM parser is better suited for small XML file with sufficient memory, while SAX parser is better suited for large XML files.


That's all on DOM vs SAX parser in XML and Java. These are best option for XML parsing in Java and requires careful decision while choosing DOM or SAX.


Source: java67

0 comments
bottom of page