StringTo格式化xml
程序员文章站
2022-03-03 13:07:54
...
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使用SAX方式解析