tags:

views:

318

answers:

1

I have a simple question about Java's XML API and I hope there's a simple answer too:

Lets say that after processing I have the following XML output:

<a>
    <b><c>
    <d> <e> some content
     </e> </d>
    </c> </b>
</a>

The structure is correct but whitespaces are all over the place. The question is that how can I prettify the output so that it looks something like this:

<a>
    <b>
     <c> 
      <d>
       <e>some content</e>
      </d>
     </c>
    </b>
</a>

Only catch is that I can't use anything but Java 5's own XML API.

+4  A: 

Use Transformer.setOutputProperty(OutputKeys.INDENT, "yes").

Bombe
Thanks, knew it'd be easy, just didn't know where to look.
Esko
Yes, it’s not that well documented. OutputKeys also has a couple of other constants for more nifty stuff. :)
Bombe
Don't work for me! The only thing that happens is that each element starts at a new line, but no indents! I have JDK6u14.
ivan_ivanovich_ivanoff