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(
"<root><a>hello</a><b>world!</b></root>"
);
Now, we have an object of interface XML
that can traverse the XML tree and convert it back to text.
For example:
// outputs "hello"
System.out.println(xml.xpath("/root/a/text()").get(0));
// outputs the entire XML document
System.out.println(xml.toString());
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()")) {
System.out.println(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/*"))
System.out.println(node);
}
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.