An XML parser is a software library or package that provides interfaces for client applications to work with an XML document. The XML Parser is designed to read the XML and create a way for programs to use XML.
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:
Types of XML Parsers
These are the two main types of XML Parsers:
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. This API is very simple to use.
Features of DOM Parser
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.
1) It supports 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.
1) It is memory inefficient. (consumes more memory because the whole XML document needs to loaded into memory).
2) 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.
Features of SAX Parser
It does not create any internal structure.
Clients does not know what methods to call, they just overrides the methods of the API and place his own code inside method.
It is an event based parser, it works like an event handler in Java.
1) It is simple and memory efficient.
2) It is very fast and works for huge documents.
1) It is event-based so its API is less intuitive.
2) Clients never know the full information because the data is broken into pieces.