XML - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

XML Parsers

XML Parsers

shape Description

Parser is a program part which represents the physical form of a data and converts it into storage form for the program. The one which reads XML Data in a XML file is referred to as XML Parser. Until unless the code is copied as a unit block blindly, every XML program should call the XML Parser. There are two kinds of XML Parsers:

Why DOM and SAX?

shape Description

If the XML file data has to be read then developer has to write their own code to parse the XML file which is complicated procedure. To avoid this, Java/programming language has given parsers like DOM and SAX to read the XML file. XML Parser consumes lot of time and it is complicated. Whereas DOM/SAX parser is used to parse the XML file. Using this, the code can be developed fast and accurate.

Difference between DOM Parser and SAX Parser

DOM Parsers SAX Parsers
DOM stands for Document Object Model SAX stands for Simple API for XML
DOM reads the entire document SAX reads node by node
DOM is useful when reading small to medium size XML files SAX is used when big XML files to be parsed
DOM is tree based parser SAX is event based parser
DOM is little slow as compared to SAX SAX is faster than DOM
DOM can insert and delete nodes SAX cannot insert and delete nodes
As shown in the above figure, DOM reads the XML file and stores in tree format. Whereas the SAX Parser will have stream of events and will call the methods like startDocument, startElement, characters, endElement, endDocument

Advantages and Dis-Advantages

shape Description

DOM Parsers

Advantages
  • DOM parsers works fine when document is divided into parts and accessed randomly.
  • Operations like read and write on a document will be performed using DOM Parsers.
Dis-Advantages
  • Memory supported by DOM parsers is inefficient.
  • DOM Parsers are complicated.

SAX Parsers

Advantages
  • Memory supported by SAX Parsers is efficient.
  • In stream applications, the functionality of SAX parsers is good.
Dis-Advantages
  • The information is divided into pieces and clients will not have all the dat auntil the users create own data structure.

Summary

shape Key Points

  • The one which reads XML Data in a XML file is referred to as XML Parser.
  • DOM/SAX parser is used to parse the XML file.