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 version="1.0" encoding="UTF-8" standalone="no"?><a><b/></a>
期望的结果 有什么
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<a>
<b/>
</a>
想法吗?
I'm using Java's built in XML transformer to take a DOM document and print out the resulting XML. The problem is that it isn't indenting the text at all despite having set the parameter "indent" explicitly.
sample code
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()));
}
}
result
<?xml version="1.0" encoding="UTF-8" standalone="no"?><a><b/></a>
desired result
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<a>
<b/>
</a>
Thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您需要启用“缩进”并设置转换器的缩进量:
更新:
参考:如何在序列化之前从 DOM 中去除纯空白文本节点?
(非常感谢所有成员,尤其是 @marc-novakowski、@james-murty 和@saad):
You need to enable 'INDENT' and set the indent amount for the transformer:
Update:
Reference : How to strip whitespace-only text nodes from a DOM before serialization?
(Many thanks to all members especially @marc-novakowski, @james-murty and @saad):
建议的解决方案都不适合我。所以我继续寻找替代解决方案,最终成为前面提到的两者和第三步的混合。
您必须执行(3)来解决该问题的“错误”行为
xml 处理代码。
资料来源:johnnymac75 @ https://bugs.java.com/bugdatabase/view_bug?bug_id= 6296446
(如果我错误地引用了我的来源,请告诉我)
Neither of the suggested solutions worked for me. So I kept on searching for an alternative solution, which ended up being a mixture of the two before mentioned and a third step.
You must do (3) to workaround a "buggy" behavior of the
xml handling code.
Source: johnnymac75 @ https://bugs.java.com/bugdatabase/view_bug?bug_id=6296446
(If I have cited my source incorrectly please let me know)
以下代码适用于 Java 7。我在变压器(不是变压器工厂)上设置了缩进(是)和缩进量(2)以使其正常工作。
@mabac 设置属性的解决方案对我不起作用,但 @lapo 的评论证明很有帮助。
The following code is working for me with Java 7. I set the indent (yes) and indent-amount (2) on the transformer (not the transformer factory) to get it working.
@mabac's solution to set the attribute didn't work for me, but @lapo's comment proved helpful.
导入 com.sun.org.apache.xml.internal.serializer.OutputPropertiesFactory
import com.sun.org.apache.xml.internal.serializer.OutputPropertiesFactory
如果您想要缩进,则必须将其指定给
TransformerFactory
。If you want the indentation, you have to specify it to the
TransformerFactory
.对我来说添加
DOCTYPE_PUBLIC
有效:For me adding
DOCTYPE_PUBLIC
worked:我使用了 Xerces (Apache) 库,而不是使用 Transformer。添加库后,添加以下代码。
I used the Xerces (Apache) library instead of messing with Transformer. Once you add the library add the code below.