Skip to main content

XML Parser

What is an XML Parser?

An XML Parser is a software library or a package that provides interfaces for client applications to work with an XML document.

Modern browsers have a built-in XML parser to access and manipulate XML.

Goal: transform XML into readable code.

Types of XML Parser

There are two main types of XML parsers:

  • DOM (Document Object Model)
  • SAX (Simple API for XML)

DOM (Document Object Model)

A DOM parser creates an internal tree structure in memory, called Document Object Model (DOM). Client applications can get information by invoking methods on this object to retrieve information contained in the original XML document.

Advantages

  • It supports both read and write operations and API is simple to use.
  • It is preferred when random access to widely separated parts of a document is required

Disadvantages

  • It is memory inefficient because the entire document must be loaded into memory.
  • It is slower than other parsers

DOM parser implements a DOM API.

SAX (Simple API for XML)

A SAX parser implements SAX API. SAX API is an event-based API and less intuitive than DOM API.

There isn't an internal structure. Clients just overrides the methods of the API to get information from the XML document.

Advantages

  • It is simple and memory efficient.
  • It is very fast and it works for huge documents.

Disadvantages

  • It is event-based, so its API is less intuitive.
  • Clients never know the full information because the data is fragmented.