Java and J2EE Tutorials, Jsp and Servlet Tutorials, Spring MVC, Solr, XML, JSON Examples, Hibernate & Struts 2 Hello World projects

Friday, 31 May 2013

Java - Xml parser - DOM, SAX, JDOM, XPATH and JAXB parsers in Xml

Today, XML has become an essential part of almost every web application. The reason behind XML’s popularity and wide usage is it’s platform independent nature and easy to use capability. Other than platform independency XML is widely used for its Data Storage and Data Validation capabilities.

In today’s discussion we will talk about Different type of Java XML parser and their implementations. Java provides full support with a number of general purpose xml readers to read, write and manipulate xml data and documents.

Some of the most commonly used java XML parser and implementation techniques are listed below :

1) DOM
2) SAX
3) JDOM2

What is XML DOM  parser in Java (Document Object Model)

DOM xml so called Document Object Model is the easiest and commonly used XML manipulation technique of today. DOM xml parser requires to copy an tree structure in memory before parsing the xml, each branch of the tree represents an element. DOM  xml parser is most preferable and fast implementation of XML, if the xml size is not so large. DOM copies xml into memory before parsing it, so it is always advisable to have a big heap size to avoid memory exceptions.

Java provides a full support for DOM parsing, DOM parser for java resides in JAXP (Java API for XML parsing) package. If the xml size is small than DOM is the best choice in java, but for large XML’s it is possible to take large amount of time while using or simply the parsing can be failed because DOM uses large amount of memory to load the XML before parsing.

We don’t need to add any external jar to use DOM, it is always available there in JDK internally.

What is XML SAX  parser in Java (Simple API for XML)

SAX xml parser so called Simple API for XML is the best choice to parse large XML’s in Java. SAX xml parser is an event based parser and a XML with any size can be parsed using SAX in java. SAX xml does not requires to load whole xml in memory, but it loads the xml in chunks and parse it accordingly.

Java provides a full support to implement SAX, it searches for opening tags of elements, tags and attributes and works accordingly. The only drawback with SAX xml parser in Java is that it needs more number of code lines to implement parsing process.

We don’t need to add any external jar to use DOM, it is always available there in JDK internally.

What is JDOM xml Parsing

JDOM2 provides and efficient alternative to DOM and XAX technologies by providing a simple and straight forward API for Java developers. JDOM2 is more proficient choice to parse and manipulate data in java, because it provides a Java centric structure and implementation of XML.

Unlike DOM and SAX, JDOM2 does not bundles in JDK internally. We need to download and include and external jar file. JDOM2 libraries can be downloaded from JDOM site Downloads.

What is JAXB (Java Architecture for XML binding)

JAXB so called java architecture for XML binding is an efficient technology to convert XML to and from Java Object. JAXB is mostly used to create java classes from XML in Java Web Services. In Java JAXB provides two general purpose implementation.

Marshalling – It Converts a Java object into XML.
Unmarshalling – It Converts XML into a Java Object.

Again JAXB is a part of JDK , we don’t  need to download or add anything extra to start. But if you are using a version less than JDK5.0 you need to add two libraries named ‘jaxb-api.jar’ and ‘jaxb-impl.jar’ to the classpath.

What is XPATH xml parsing

XPATH is a more advanced technique to parse and extract required data from XML. XPATH provides an extensive support for query based extraction system to get more accurate data from XML documents.

XPATH is similar to SQL in context of query, it provides a powerful set of expressions to parse and extract data from xml. Java provides full support for XPATH implementation, all classes required by XPATH can be found under ‘javx.xml.xpath.*’ package. We don’t need to download or add anything else.

In today's discussion we came across Java Xml parsing implementations and different between various java xml parser. In upcoming blogs we will see different xml readers in java and a detailed explanation of DOM, SAX, JDOM2, XPATH and JAXB technologies and example in Java. Read more about XML & JSON under SAX & DOM & JDOM2 & JSON, and JAXB - Marshalling & Unmarshalling in Java categories.

Thanks for reading !
Being Java Guys Team


  1. Thanks for this presentation, i will have a better understanding of java/xml technologies !

  2. Thanks, Visit also, for various interview related Question and answers..

  3. What is difference between XML parsing and web services. Is xml parsing same as consuming web services


Like Us on Facebook

Like Us On Google+


Skype: neel4soft