tags:

views:

857

answers:

5

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 Google'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 Google'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.

+5  A: 

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;
Thanks, I hadn't considered dom4j.
Jim Ferrans
A: 

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

CurtainDog
I need to write an object to XML mapping where the XML is a fixed standard (CableLabs' ADI 1.1) and the Java classes are also pre-ordained.
Jim Ferrans
Use JAXB to map java classes to the Schema and then use Dozer to map from the JAXB classes to the pre-ordained java classes.
Jherico
@Jherico: Thanks, haven't seen Dozer yet. So you could take the desired schema and use JAXB to create a corresponding set of JavaBean classes from them, then set up a Dozer mapping from the existing JavaBeans to the JAXB JavaBeans. Sounds like more code but also more automation. I see what CurtainDog means now.
Jim Ferrans
A: 

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.

Tom
A: 

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).

StaxMan
+1  A: 

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

deterb