Difference between DOM and SAX parser in Java

DOM XML parser in Java

DOM stands for Document Object Model and represents an XML document in tree format, each element representing tree branches. The XML file will be loaded as a whole and all its contents will be built as an in-memory representation of the tree the document represents.

Parsing XML file using DOM parser is quite fast if XML file is small but if you try to read a large XML file using DOM parser then it is possible that it will take a long time or even may not be able to load it completely simply because it requires lot of memory to create XML Dom Tree. The benefit using DOM is that any part of the document can be checked, selected and manipulated.

SAX parser in Java

SAX Stands for Simple API for XML Parsing. When the parser is parsing the XML, and encounters a tag starting (e.g. ), then it triggers the tagStarted event (actual name of event might differ). Similarly when the end of the tag is met while parsing (), it triggers tagEnded. This type of parsing is suitable for large XML files because it doesn’t require to load whole XML file.

Difference between DOM and SAX XML Parser

  • DOM parser loads whole XML document in memory while SAX only loads a small part of the XML file in memory.
  • DOM parser is faster than SAX because it loads whole XML document in memory.
  • Since DOM loads whole document in memory so it is memory dependent and is not suitable for large XML files. Whereas SAX is event based and loads only a part of XML file in memory so it is suitable for large XML files.
  • DOM parser works on Document Object Model while SAX is an event based XML parser. In DOM, there are no events triggered while parsing. The entire XML is parsed and a DOM tree is generated.
  • DOM can insert or delete nodes. SAX is read only.
  • Backward and forward search is possible in DOM parser and you can query tags of whole document. So navigation is easier in DOM.
    SAX reads document from top to bottom, so backward navigation is not possible.