How to pretty print XML from Java?

后端 未结 30 2470
慢半拍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:47

    Underscore-java has static method U.formatXml(string). I am the maintainer of the project. Live example

    import com.github.underscore.lodash.U;
    
    public class MyClass {
        public static void main(String args[]) {
            String xml = "hello";
    
            System.out.println(U.formatXml("" + xml + ""));
        }
    }
    

    Output:

    
    
       
          hello
       
    
    

提交回复
热议问题