Java xml parser

Java has a set of XML parsers. Each of these parsers is a standalone XML component that parses an XML document (or a standalone DTD or XML Schema) so that it can be processed by an application.

Java xml parser

Xml parser basic process

  1. The DOM or SAX parser parses input XML documents. For example, the program can parse XML data documents, DTDs, XML schemas, and XSL stylesheets.
  2. If we implement a validating parser, then the processor attempts to validate the XML data document against any supplied DTDs or XML schemas.

Dom parser

Java Dom parser api for xml is intended for working with XML as an object graph (a tree like structure) in memory – so called "Document Object Model (DOM)".

Dom parser traverses the input XML file and creates DOM objects corresponding to the nodes in XML file. These DOM objects are linked together in a tree like structure. Once the parser is done with parsing process, you get this tree-like DOM object structure back from it. Now you can traverse the DOM structure back and forth as you want to get/update/delete data from it.

Dom parser with example

Sax parser

Sax, or Simple Api for xml is an event-driven model (you provide the callback methods, and the parser invokes them as it reads the XML data), and that makes it harder to visualize. Finally, you cannot "back up" to an earlier part of the document, or rearrange it, any more than you can back up a serial data stream or rearrange characters you have read from that stream.

Sax parser with example

Stax parser

StAX parser was created to address limitations in the two most prevalent parsing APIs, SAX and DOM. The primary goal of the StAX API is to give "parsing control to the programmer by exposing a simple iterator based API. This allows the programmer to ask for the next event (pull the event) and allows state to be stored in procedural fashion."

Stax parser with example

Share & Like

About is for Java, J2ee, Html5 and android developers. All tutorials are well explained and are easy to understand. We have tested every tutorials on our developement environment before posting it in public domain.