How to pretty print XML from Java?

后端 未结 30 3177
慢半拍i
慢半拍i 2020-11-22 01:55

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?



        
30条回答
  •  忘掉有多难
    2020-11-22 02:43

    In case you do not need indentation that much but a few line breaks, it could be sufficient to simply regex...

    String leastPrettifiedXml = uglyXml.replaceAll("><", ">\n<");
    

    The code is nice, not the result because of missing indentation.


    (For solutions with indentation, see other answers.)

提交回复
热议问题