Friday, May 13, 2016

XML parser

XML parser is a software library (or package) that reads and parses XML. It is designed to read XML and create a way for programs. Different types of XML parses are available for various languages.

XML parser validates the document and check that the document is well formatted.

Let's understand the working of XML parser by the figure given below:

Main Types of XML parsers in Java

DOM (Document Object Model)

  • A DOM document is an object which contains all the information of an XML document. It is composed like a tree structure. The DOM Parser implements a DOM API.
  • A DOM Parser creates an internal structure in memory which is a DOM document object and the client applications get information of the original XML document by invoking methods on this document object.
  • DOM Parser has a tree based structure.
  • It supports both read and write operations and the API is very simple to use.
  • It is preferred when random access to widely separated parts of a document is required.
  • It is memory inefficient. (consumes more memory, whole XML document needs to loaded into memory).
  • It is comparatively slower than other parsers.

SAX (Simple API for XML)

  • A SAX Parser implements SAX API. This API is an event based API and less intuitive. it reads each unit of XML, it creates an event which can be used by calling program. It is used for high performance applications or areas where XML size might exceed the available memory. SAX reads the XML documents as a stream of XML tags(starting elements, ending elements, text sections etc). Programmer decides what to do with every event. SAX parser does not create any object at all, it simply delivers events.
  • It does not create any internal structure.
  • It is simple and memory efficient.
  • It is very fast and works for huge documents.
  • Here data is broken and client never have all the information.

StAX: Streaming API for XML

StAX is a pull parser. his parser pulls the required data from XML. It can give a significance performance improve. It maintains a cursor at the current position.

Difference between SAX parser and StAX parser:
  1. The SAX parser pushes the data whereas StAX parser pulls the data from XML
  2. StAX parser maintains a cursor at the current position to exract the content available at the cursor but SAX parser issues event when some certain data is encountered

No comments:

Post a Comment