StringTo格式化xml

阅读更多

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)