Skip to content
Advertisement

How to pretty print XML from Java?

I have a Java String that contains XML, with no line feeds or indentations. I would like to turn it into a String with nicely formatted XML. How do I do this?

JavaScript

Note: My input is a String. My output is a String.

(Basic) mock result:

JavaScript

Advertisement

Answer

Now it’s 2012 and Java can do more than it used to with XML, I’d like to add an alternative to my accepted answer. This has no dependencies outside of Java 6.

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement