XML Parser

tutorial and example
tutorial and example

XML- Parser is a software library that provides interface for client applications to work with XML documents. It checks for proper format of XML document and also validate the xml document. Now day’s browsers are build-in XML parsers. The goal of a parser is to transform XML into a readable code.

Following diagram shows the working of XML parser-

Types of Parsers

There are two main types of XML Parsaers:

DOM (Document object Model)

It is an object which contains all the information of an XML Document. It is tree-based approach to navigating an XML document. It implements a DOM API and the API is simple to use.

Advantages

  1. It support both read and write operations and the API is very simple to use.
  2. It is preferred when random access to widely separated parts of a document is required.

Disadvantages

  1. It is slower and more memory consuming because the whole XML needs to be loaded into memory.
  2. It is slower than others parsers.

SAX (Simple API for XML)

A SAX specification defines an event-based approach whereby parsers scan through XML data, calling handler functions whenever certain parts of the document (e.g., text node or processing instructions) are found.

Advantages

  1. It is simple and easy to understand.
  2. It is used to work for huge documents.

Disadvantages

  1. It has limited API.
  2. Only traverse the document once.