7
Java:如何缩进由Transformer生成的XML
我正在使用Java的内置XML转换器来获取DOM文档并打印出结果XML。问题在于,尽管已显式设置参数“ indent”,但根本不缩进文本。 样例代码 public class TestXML { public static void main(String args[]) throws Exception { ByteArrayOutputStream s; Document d = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); Transformer t = TransformerFactory.newInstance().newTransformer(); Element a,b; a = d.createElement("a"); b = d.createElement("b"); a.appendChild(b); d.appendChild(a); t.setParameter(OutputKeys.INDENT, "yes"); s = new ByteArrayOutputStream(); t.transform(new DOMSource(d),new StreamResult(s)); System.out.println(new String(s.toByteArray())); } } 结果 <?xml …
112
java
xml
transform
indentation