Question

I need to build an XML document from a Java object hierarchy. Both the Java classes and the XML format are fixed. So I can't use an XML serializer like XStream: it bases the XML format on the Java classes. Likewise, a Java XML binding technology like JAXB won't work, since it creates Java classes from the XML schema [ed: but see below]. I need a manual approach.

The low-tech StringBuilder route results in fragile and buggy code (at least for me!).

An API like JAXP or JDOM leads to much more robust code, but these are pretty verbose.

Groovy has an elegant MarkupBuilder:

def writer = new StringWriter()
def xml = new MarkupBuilder(writer)
xml.records() {
  car(name:'HSV Maloo', make:'Holden', year:2006) {
    country('Australia')
    record(type:'speed', 'Production Pickup Truck with speed of 271kph')
  }
  car(name:'P50', make:'Peel', year:1962) {
    country('Isle of Man')
    record(type:'size', 'Smallest Street-Legal Car at 99cm wide and 59 kg')
  }
}

Other languages (eg. Ruby) have even better ones, though I want to stay with pure Java. There do seem to be some new XML builders for Java, such as practicalxml and James Murty's xmlbuilder.

What are the more elegant approaches for building XML documents in Java?

Summary:

Jon Doe suggested dom4j and jdom.

CurtainDog recommended using JAXB anyway, and jherico clued me in that this was a pertinant suggestion: you could then use Dozer to map between my current JavaBeans and the JAXB JavaBeans.

thaggie recommends JIBX and agreed with CurtainDog and jherico that binding technologies are actually practical.

StaxMan recommends StaxMate.

Of the stuff I've looked at, practicalxml and James Murty's xmlbuilder seem to be the most concise builders, though are rather newish. The binding technologies like JAXB seem to offer extra safety/automation. Of the mainstream choices, dom4j seems decent, though still kind of verbose. It offers a "fluent interface" (mutators return a reference to the mutated object so they can be chained together), which I like:

public Document createDocument() {
    Document document = DocumentHelper.createDocument();
    Element root = document.addElement( "root" );
    Element author2 = root.addElement( "author" )
      .addAttribute( "name", "Toby" )
      .addAttribute( "location", "Germany" )
      .addText( "Tobias Rademacher" );
    Element author1 = root.addElement( "author" )
      .addAttribute( "name", "James" )
      .addAttribute( "location", "UK" )
      .addText( "James Strachan" );
    return document;
}

For conciseness, you could wrap a thin facade over this API to provide terse synonyms for some of these methods (eg, attr() instead of addAttribute()).

Thanks all!

P.S.: Stephan Schmidt worked on a Java MarkupBuilder, though seems not to have published it.

Was it helpful?

Solution

dom4j or jdom are probably the most elegant, you can write code how you like it. Dom4j has builders if I recall, and yes the code is more verbose.

Element.addElement("x").setAttribute("x", "y").xxxxx;

OTHER TIPS

Take a look at XOM. It's fast, simple, correct, and isn't verbose.

Why don't you just use JAXB anyway.. then the problem becomes a very simple object to object mapping and you avoid xml altogether.

While not quite as concise as builders in scripting languages, StaxMate makes things quite simple; generally as simple as tree models structurally, but it additionally supports typed addition (implicit conversions). And does this all directly into a stream, meaning very low memory usage (and high speed if that matters).

For what it's worth, it also supports fluent style (as of 2.0.x), since it does often make sense. The main benefit over full data binding (and tree model) solutions is probably low memory usage; very little state is kept around, all output goes out to destination as soon as possible.

You may be able to consider JIBX, you may be able to define a mapping from your domain model classes to your target XML schema.

Alternatively, if that's not possible, although I know you state that you've discounted using binding technologies I'd encourage you to review that decision, copying from your domain model into a generated model will most likely make for cleaner, more maintainable and less error prone code than what you're proposing, (which JIBX can also do).

I should probably add, in my experience asking questions about JIBX here is fruitless but their mailing list is very helpful.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top