欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

StringTo格式化xml

程序员文章站 2022-05-29 20:03:44
...

import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.io.OutputFormat; import org.dom4j.io.XMLWriter;

 

public static String formatXml(String str) throws Exception {

Document document = null;

document = DocumentHelper.parseText(str);

// 格式化输出格式

OutputFormat format = OutputFormat.createPrettyPrint();

format.setEncoding("utf-8");

format.setIndent(true);

StringWriter writer = new StringWriter();

// 格式化输出流

XMLWriter xmlWriter = new XMLWriter(writer, format);

// 将document写入到输出流

xmlWriter.write(document);

xmlWriter.close();

 

return writer.toString();

}

 

相关标签: xml