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:
Now, we have an object of interface
XML that can traverse the XML tree and convert it back to text.
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:
nodes() enables the same XPath search operation, but instead returns a collection of instances of
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.