java xml pretty printing - preserve empty elements and white pace

Posted by javamonkey79 on Stack Overflow See other posts from Stack Overflow or by javamonkey79
Published on 2010-05-06T20:22:04Z Indexed on 2010/05/06 20:28 UTC
Read the original article Hit count: 370

Filed under:
|

Basically, I am looking for a java library that will take this:

<foo><bar>   </bar><baz>yadda</baz></foo>

And pretty print it to this:

<?xml version="1.0" encoding="UTF-8"?>

<foo>
  <bar>   </bar>
  <baz>yadda</baz>
</foo>

e.g. preserving whitespace AND blank elements

The closest I have got was with dom4j like so:

        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setTrimText( false );

However, this does not honor the whitespace unless the element contains other character data.

I'm not opposed to writing something on my own, but I would think this has already been done, why reinvent the wheel?

© Stack Overflow or respective owner

Related posts about java

Related posts about Xml