因为移动到java 1.7 Xml文档元素不缩进 [英] Since moving to java 1.7 Xml Document Element does not indent
问题描述
除了第一个节点 - 文档元素,所有DOM节点都按照预期缩进。
文档元素不会在新行中启动,只是紧跟在XML声明之后。
当我转到java 1.7时,当使用java时1.6或1.5它不会发生。
我的代码:
ByteArrayOutputStream s = new OutputStreamWriter(out,utf-8);
TransformerFactory tf = TransformerFactory.newInstance();
变压器变压器= tf.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT,yes);
transformer.setOutputProperty({http://xml.apache.org/xslt}indent-amount,4);
transformer.transform(new DOMSource(doc),新的StreamResult(s));
输出:
<?xml version =1.0encoding =UTF-8?>< a>
< b> bbbbb< / b>
< / a>
任何人都知道为什么?
btw,
当我添加属性
transformer.setOutputProperty(OutputKeys.STANDALONE,yes);
它按预期工作,但xml声明已更改,
现在具有独立属性,我不想更改xml声明。
好的,
我在Java API中找到这个:
如果指定了doctype-system属性,则xml输出方法应输出文档类型声明在第一个元素之前。
SO我使用这个属性
transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC,yes);
它解决了我的问题,改变了我的xml声明。
谢谢。
I'm trying to indent XML which generated by Transformer. All the DOM Node are being Indent as expected except for the First Node - The Document Element. document element does not start in a new line , just concat right after the XML Declaration.
This bug arise when I moved to java 1.7 , when using java 1.6 or 1.5 it does not happen.
My code :
ByteArrayOutputStream s = new OutputStreamWriter(out, "utf-8");
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount","4");
transformer.transform(new DOMSource(doc), new StreamResult(s));
The output:
<?xml version="1.0" encoding="UTF-8"?><a>
<b>bbbbb</b>
</a>
Anyone knows why ?
btw, when I add the property
transformer.setOutputProperty(OutputKeys.STANDALONE, "yes");
It work as expected , but the xml declaration is changed, it now have the standalone property as well, and i don't want to change the xml declaration..
Ok ,
I found in Java API this :
If the doctype-system property is specified, the xml output method should output a document type declaration immediately before the first element.
SO I used this property
transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, "yes");
and it solve my problem with out changed my xml declartion.
Thanks.
这篇关于因为移动到java 1.7 Xml文档元素不缩进的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!