1. Overview of DOM and SAX parser in Java
The objective of DOM (Document Object Model) parser and SAX (Simple API for XML) parser are same but implementation are different. Both the parser work in different way internally, but intent of both are same. Internal implementation of DOM Vs SAX are different. It means, with same intent philosophy of the implementation are different.
In order to understand the difference between DOM and SAX, you have to understand each one of the parsers. For this you can visit DOM Parser: how to read XML in JAVA.
2. Key Difference of DOM and SAX
You should know the key differences about DOM and SAX Parser.
- DOM stands for Document Object Model while SAX stands for Simple API for XML parsing.
- 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.
- If you know you have sufficient amount of memory in your server you can choose DOM as this is faster because load entire XML in-memory and works as tree structure which is faster to access.
- As a thumb rule, for small and medium sized XML documents, DOM is much faster than SAX because of in memory agnostic.
- As a thumb rule, for larger XML and for frequent parsing, SAX XML parser is better because it consume less memory.
3. DOM Vs SAX Parser
|DOM (Document Object Model)||SAX (Simple API for XML) Parser|
|Abbreviation||DOM stands for Document Object Model,||SAX stands for Simple API for XML Parsing|
|type||Load entire memory and keep in tree structure||event based parse|
|size of Document||good for smaller size||good to choose for larger size of file.|
|Load||Load entire document in memory||does not load entire document.|
|suitable||better suitable for smaller and efficient memory||SAX is suitable for larger XML doc|
4. Related Posts of DOM and SAX Parser
In order to understand the difference between DOM and SAX, you have to understand each one of the parsers. For this you can visit.
- DOM Parser: how to write XML in JAVA
- DOM Parser: how to read XML in JAVA
- DOM Parser: Schema Validation in JAVA
- SAX Parser: Schema Validation in JAVA
- Different XML parsers Comparison
You can visit Java Site
I hope you enjoyed this post about DOM and SAX parser in java. You can visit Core Java tutorial for more example blog post.
Your comments are welcome. Happy Learning 🙂