Java怎么从格式化显示Xml内容

走着路睡觉
  • java
小于 1 分钟


Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
// 设置打印 <?xml version="1.0" encoding="UTF-8"?>
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes")
// 输出到哪
StreamResult result = new StreamResult(new StringWriter());
//doc文本
DOMSource source = new DOMSource(doc);
transformer.transform(source, result);
String xmlString = result.getWriter().toString();
System.out.println(xmlString);

上次编辑于:
贡献者: zhaojingbo
Loading...