Getting nicely indented XML from XOM as a String is not intuitive. One might reasonably expect to try document.toXML() only to find the output lacking indentation.

Here’s how I used XOM’s Serializer and Java’s ByteArrayOutputStream to produce pretty XML.

1
2
3
4
5
ByteArrayOutputStream out = new ByteArrayOutputStream();
Serializer serializer = new Serializer(out, "UTF-8");
serializer.setIndent(2);
serializer.write(document);
String xml = out.toString("UTF-8");

 Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">

   
© 2011 And now here's something… Suffusion theme by Sayontan Sinha