Write XML to a file Steps to create and write XML to a file. Create a Document doc . Create XML elements, attributes, etc., and append to the Document doc . Create a Transformer to write the Document doc to an OutputStream .
Read and Write XML with JAXB. JAXB stands for Java Architecture for XML Binding which provides a convenient way for manipulating XML in Java. It is Java standard that defines an API for reading and writing Java objects to and from XML documents. Starting from Java 6, JAXB is a part of the Java Development Kit (JDK).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With