将单行xml字符串格式化
代码如下,只需将resultXML中的字符串替换成要格式化的字符串便可;出现下划线高亮/报错请检查是否导入了对应的包和类!!!
String resultXML = "<site><name>格式化XML</name><url>https://www.runoob.com/xml/xml-tutorial.html</url><logo>runoob-logo.png</logo><desc>编程学习网站</desc></site>";
XmlOptions options = new XmlOptions();
options.setSavePrettyPrint();
options.setSavePrettyPrintIndent(4);
System.out.println("XML格式化前"+resultXML);
try {
System.out.println(XmlObject.Factory.parse(resultXML).xmlText(options));
} catch (XmlException e) {
throw new RuntimeException(e);
}
输出结果:
![](https://img-blog.csdnimg.cn/direct/937ed8006643459c9cc05f75b52114d4.png)
至此,xml格式化完成!!!