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?
String unformattedXml = "<tag><nested>hello</nested></tag>";
String formattedXml = new [UnknownClass]().format(unformattedXml);
Note: My input is a String. My output is a String.
(Basic) mock result:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<tag>
<nested>hello</nested>
</tag>
</root>
Since you are starting with a
String
, you need to covert to aDOM
object (e.g.Node
) before you can use theTransformer
. However, if you know your XML string is valid, and you don't want to incur the memory overhead of parsing a string into a DOM, then running a transform over the DOM to get a string back - you could just do some old fashioned character by character parsing. Insert a newline and spaces after every</...>
characters, keep and indent counter (to determine the number of spaces) that you increment for every<...>
and decrement for every</...>
you see.Disclaimer - I did a cut/paste/text edit of the functions below, so they may not compile as is.
Just another solution which works for us
Just to note that top rated answer requires the use of xerces.
If you don't want to add this external dependency then you can simply use the standard jdk libraries (which actually are built using xerces internally).
N.B. There was a bug with jdk version 1.5 see http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6296446 but it is resolved now.,
(Note if an error occurs this will return the original text)
slightly improved version from milosmns...
a simpler solution based on this answer:
testcase:
returns:
If using a 3rd party XML library is ok, you can get away with something significantly simpler than what the currently highest-voted answers suggest.
It was stated that both input and output should be Strings, so here's a utility method that does just that, implemented with the XOM library:
I tested that it works, and the results do not depend on your JRE version or anything like that. To see how to customise the output format to your liking, take a look at the
Serializer
API.This actually came out longer than I thought - some extra lines were needed because
Serializer
wants anOutputStream
to write to. But note that there's very little code for actual XML twiddling here.(This answer is part of my evaluation of XOM, which was suggested as one option in my question about the best Java XML library to replace dom4j. For the record, with dom4j you could achieve this with similar ease using
XMLWriter
andOutputFormat
. Edit: ...as demonstrated in mlo55's answer.)