Monday, May 15, 2023

Difference between DOM and SAX Parsers in Java

A difference between SAX and DOM Parser is very popular Java interview and often asked when interviewed on Java and XML. Both DOM and SAX parser is extensively used to read and parse XML file in java and has their own set of advantages and disadvantages which we will cover in this article. Though there is another way of reading XML files using XPath in Java which is the more selective approach like SQL statements people tend to stick with XML parsers. DOM Parser vs SAX parsers is also often viewed in terms of speed, memory consumption, and their ability to process large XML files.


Difference between DOM and SAX XML Parser in Java

Now let see some key differences between both DOM and SAX parser while parsing or reading XML files in Java.


1. DOM XML Parser in Java

DOM Stands for Document Object Model and it represents an XML Document in tree format which each element representing tree branches. DOM Parser creates an In Memory tree representation of XML file and then parses it, so it requires more memory and it's advisable to have increased the heap size for DOM parser in order to avoid Java.lang.OutOfMemoryError:java heap space

Parsing XML file using DOM parser is quite fast if the XML file is small but if you try to read a large XML file using DOM parser there are more chances that it will take a long time or even may not be able to load it completely simply because it requires a lot of memory to create XML Dom Tree. 

Java provides support DOM Parsing and you can parse XML files in Java using DOM parser. DOM classes are in w3c.dom package while DOM Parser for Java is in JAXP (Java API for XML Parsing) package.



2. SAX XML Parser in Java

SAX Stands for Simple API for XML Parsing. This is an event based XML Parsing and it parse XML file step by step so much suitable for large XML Files. SAX XML Parser fires an event when it encountered opening tag, element or attribute, and the parsing works accordingly. 

It’s recommended to use SAX XML parser for parsing large XML files in Java because it doesn't require loading the whole XML files in Java and it can read a big XML file in small parts. 

Java provides support for SAX parser and you can parse any XML file in Java using SAX Parser, I have covered an example of reading XML file using SAX Parser here. One disadvantage of using SAX Parser in java is that reading XML files in Java using SAX Parser requires more code in comparison to DOM Parser.


Difference between DOM and SAX XML Parser

Here are a few high-level differences between DOM parser and SAX Parser in Java:

1) DOM parser loads whole XML documents in memory while SAX only loads a small part of the XML file in memory.

2) DOM parser is faster than SAX because it accesses the whole XML document in memory.

3) SAX parser in Java is better suitable for a large XML file than DOM Parser because it doesn't require much memory.

4) DOM parser works on Document Object Model while SAX is an event-based XML parser.

Here is a nice diagram which highlight not just difference between DOM and SAX parser but also Stax parser:

Difference between DOM and SAX Parsers in Java



difference between DOM and XML parsers in JavaThat’s all on the difference between SAX and DOM parsers in Java, now it’s up to you on which XML parser you going to choose. I recommend using DOM parser over SAX parser if the XML file is small enough and go with SAX parser if you don’t know the size of XML files to be processed or they are large.


Java Tutorial you may like:

12 comments :

Akhileshwar said...

When do you choose DOM vs SAX parser in Java ? From this post I see choose SAX vs DOM parser when XML file is Big, Java heap is limited and performance is not a concern. While Use DOM vs SAX parser if XML document is small, you have enough Java memory or heap and performance is important i.e. you want to parse XML document quickly. Does these observations are correct ?

Anonymous said...

Where are you getting this from?

2) DOM parser is faster than SAX because it access whole XML document in memory.

Isn't it the other way round?

Aparna said...

Yes the SAX parser is faster then DOM that is what the writer had explained before the point.

2) DOM parser is faster than SAX because it access whole XML document in memory.

I think it was just a typo

Anonymous said...

by comparing memory based performance, SAX is better is it correct?

Unknown said...

"DOM parser is faster than SAX because it access whole XML document in memory" for the first look,it seems to be wrong but writers actually wants to say it uploads all data into the memory for once than it works fine until the data overflows from memory.Summary for large data files Sax is better than dom.

Unknown said...

which parser should we use when we have attachments in xml file??

Unknown said...

Can you please elaberate your second point of dom being faster because of being inmemory? As far as i understand dom create a in memory hierarchical representation of xml after parsing while sax only parse it and never represent any xml.

Unknown said...

Unless and until you dont want to recreate the same xml file again with minor modification sax parser is recomanded. Infact for all other case sax with jaxb.
Point to note is writing a parser using sax alone is not as easy as transversing dom.

Unknown said...

When should we prefer the JAXB ? DOM SAX and JAXB

Unknown said...

I understand from your post that for large xml files its better to use SAX but by large you mean how much large? .

Also when to use JAXB?

Nagesh said...

My javanet connection on scratch day

Nagesh said...

I understand from your post that for large xml files its better to use SAX but by large you mean how much large? .

Post a Comment