Skip to main content

Truth about "format-pretty-output"

Posted by iasandcb on June 15, 2006 at 11:08 PM PDT

I got so much feedback from my recent blog at http://weblogs.java.net/blog/iasandcb/archive/2006/06/serializing_xml.html, and it inspired me to write another.

The most noticeable comment on the blog is to use JAXP transformer to serialize Document, and in fact I couldn't agree on the solution more because I also used it.

Apart from its standard feature, transformer gives you one more nice option: make your Document more human-readable with so-called "pretty-output". The option is so popular that it is almost essential when the output should be seen by a human being, not a machine.

Transformer is basically designed to visualize abstract XML infosets, so it is no wonder to have a diversity of presentations. Even though we do canonicalization on occasion, some want 2 spaces for indentation, and some want no self-closing tags, and so on.

That strength really works for serialization of Document. On the other hand, DOM L3 LSSerializer also supports "format-pretty-output", but unfortunately you can't expect it from JAXP RI, which is also built in Java SE 5 and 6. There's an issue posted for the problem and a possible solution at https://jaxp.dev.java.net/issues/show_bug.cgi?id=6. The patch was actually applied to Apache Xerces, so the option is supported by Xerces 2.8.0.

To sum them up, Transformer can be your friend if you like to control your outputs in details. In addition to that, it's worthwhile considering taking advantage of brevity from LSSerializer.

Related Topics >>