This is a mobile version, full one is here.

Yegor Bugayenko
24 April 2014

Java XML Parsing Made Easy

Unlike with many other modern languages, parsing XML in Java requires more than one line of code. XML traversing using XPath takes even more code, and I find this is unfair and annoying.

I’m a big fan of XML and use it it in almost every Java application. Some time ago, I decided to put all of that XML-to-DOM parsing code into a small library—jcabi-xml.

Put simply, the library is a convenient wrapper for JDK-native DOM manipulations. That’s why it is small and dependency-free. With the following example, you can see just how simple XML parsing can be:

import com.jcabi.xml.XML;
import com.jcabi.xml.XMLDocument;
XML xml = new XMLDocument(

Now, we have an object of interface XML that can traverse the XML tree and convert it back to text.

For example:

// outputs "hello"
// outputs the entire XML document

Method xpath() allows you to find a collection of text nodes or attributes in the document, and then convert them to a collection of strings, using XPath query:

// outputs "hello" and "world"
for (String text : xml.xpath("/root/*/text()")) {

Method nodes() enables the same XPath search operation, but instead returns a collection of instances of XML interface:

// outputs "<a>hello</a>" and "<b>world</b>"
for (XML node : xml.nodes("/root/*"))

Besides XML parsing, printing and XPath traversing, jcabi-xml also provides XSD validation and XSL transformations. I’ll write about those features in the next post.

PS. Also, check this: XML/XPath Matchers for Hamcrest.