XML Utility Library

As part of some open source stuff I've been doing on the side I've had to generate and parse a lot of XML. I like working with the DOM because it's tree structure cleanly matches my needs, but the W3C API is *so* cumbersome. The DOM was designed to be implemented in any language, not just clean OO languages like Java, so any code using it will work but be ugly. After considering a few other XML libraries I decided to write a new one that would work with modern Java 5 language features like generics, enhanced for-each, and varargs. This library is super tiny because it simply wraps the standard javax.xml libraries in the JRE, but gives you a much nicer interface to work with. Here's how to use it (or download it here):

Generating XML

The XMLWriter class provides methods start() and end() to generate nested XML elements. start() has var args to let you set an unlimited number of attributes on your element. Ex: to write out the element foo with attributes version=0.01 and type=bar, do the following:

out.start("foo","version","0.01","type","bar");

XMLWriter also uses method chaining to let you start and end an element on the same line. Here is a complete XML of generating XLM to the foo.xml file with a standard XML header, var args, and method chaining:

XMLWriter out = new XMLWriter(new File("foo.xml"));
out.header();
out.start("foo", "version","0.01","type","bar");
for(int i=0; i<3; i++) {
out.start("bar","id",""+i).end();
}
out.end();
out.close();

produces:

contents of foo.xml

<?xml version="1.0"?>
<foo
version='0.01'
type='bar'
>
<bar id='0'
>
</bar>
<bar id='1'
>
</bar>
<bar
id='2'
>
</bar>
</foo>

Parsing XML

The XMLParser class uses a DOM Parser and XPath to extract the parts of the document you want. Combined with generics and iterators you can conveniently parse your XML in a loop. For example, to parse the document from the previous example back in, grabbing all of the bar elements, then print out their id attributes:

Doc doc = XMLParser.parse(new File("foo.xml"));
for(Elem e : doc.xpath("//bar")) {
System.out.println("id = " + e.attr("id"));
}

Details

This XML library uses the standard W3C Dom and javax.xml parsers underneath. Each DOM element is wrapped by a custom class with the convenience methods. Only elements returned from an XPath query are wrapped, so if you skip most of the document then most of it will never get wrapped. The Doc and Elem objects have references to the underlying W3C DOM objects.

I have no real plans for this library. I just found it useful for me and thought you might be interested in it. I'll release new versions as I fix bugs and add (tiny) features.

Docs and download here

Talk to me about it on Twitter

Posted June 28th, 2010

Tagged: java.net