Xr's blog

To content | To menu | To search

Tag - xml

Entries feed

Saturday, February 14 2009

Java, XML and indenting (continued)

This is a followup to Java, XML and indenting.

More fun in the "Why doesn't my XML source get indented" series.

When you build a StreamResult and you want indentation, instanciate it with a Writer. If you use a regular OutputStream (like System.out, for example), you will have new lines but no indentation.

Of course, it's not documented. The Javadoc for StreamResult(Writer) only says:

Construct a StreamResult from a character stream. Normally, a stream should be used rather than a reader, so that the transformer may use instructions contained in the transformation instructions to control the encoding. However, there are times when it is useful to write to a character stream, such as when using a StringWriter.

See those useful times ? I guess it's what they're talking about.

Thursday, September 11 2008

Java, XML and indenting

A while ago, a friend/colleague was having trouble outputting formatted XML with Java's XML API. Both our codes output valid XML, but only mine was indenting it.

Basically, they looked like this (some logic skipped for ease of reading):

Continue reading...