XML Parsers

What is XML Parsers?

XML parsers are libraries used for checking and validating XML document schema.

DOM Parser

  • DOM (Document Object Model) provides a programming interface for manipulating XML documents.
  • DOM includes a set of objects and interfaces, which represent the content and structure of an XML document and enables a program to traverse XML tree structure.
  • DOM allows to create new XML document with programming interfaces, for example, we can create XML document through ASP.net.

SAX Parser

  • SAX stands for Simple API for XML.
  • API (Application Programming interface) allows programmer to read and write XML data.
  • SAX parser is based on events.
  • SAX Parser follows a push model which allows sequential access.

SAX Parser vs DOM Parser.

SAX ParserDOM Parser
Instead of creating internal structure in the document, it takes the occurrences of components as events.DOM parser creates a tree structure in memory from an input document and waits for client request.
SAX Parser allows to extract the information in the document according to the users interest.User can not extract the information of his interest.
SAX parser is space efficient.DOM Parser is space inefficient.
Users have to create their own data structures.DOM parser allows user to access any part of the document and modify the DOM tree.

When should you use SAX and DOM Parser?

Example:
Assume that University has an XML document in which all the information of MCA students with their marks is there. It wants to assign final grades to the student and produce a list with Exam No and grades.

Case1: If University decides to give 'A' to those who earned class average and, 'B' to others.
Case2: If University decides to give A to those students who got 65% marks or more and B to others.

  • We can use DOM Parser for Case1. For this, the university has to look out for the marks and calculate the average. They can then assign the grades by comparing the marks of all students.
  • We can use SAX Parser in Case2. For this, there is no need to go through the whole document. A grade is assigned to a student once the SAX parser reads the marks of the students.