简体   繁体   English

由于迁移到java 1.7 Xml文档元素不会缩进

[英]Since moving to java 1.7 Xml Document Element does not indent

I'm trying to indent XML which generated by Transformer. 我正在尝试缩进由Transformer生成的XML。 All the DOM Node are being Indent as expected except for the First Node - The Document Element. 除了第一个节点 - 文档元素之外,所有DOM节点都按预期缩进。 document element does not start in a new line , just concat right after the XML Declaration. document元素不会在新行中开始,只是在XML声明之后立即生成。

This bug arise when I moved to java 1.7 , when using java 1.6 or 1.5 it does not happen. 当我转移到java 1.7时会出现这个错误,当使用java 1.6或1.5时它不会发生。

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.. 它按预期工作,但xml声明已更改,它现在也具有独立属性,我不想更改xml声明..

Ok , 好 ,

I found in Java API this : 我在Java API中找到了这样的:

If the doctype-system property is specified, the xml output method should output a document type declaration immediately before the first element. 如果指定了doctype-system属性,则xml输出方法应在第一个元素之前输出文档类型声明。

SO I used this property 所以我使用了这个属性

 transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, "yes");

and it solve my problem with out changed my xml declartion. 它解决了我的问题,改变了我的xml声明。

Thanks. 谢谢。

Xalan has at some point changed the behavior regarding the newline character after the XML declaration. 在XML声明之后,Xalan在某些时候改变了关于换行符的行为。

OpenJDK (and thus Oracle JDK, too) has implemented a workaround for this problem. OpenJDK(以及Oracle JDK)也针对此问题实施了一种解决方法 This workaround can be enabled by setting a special property on the Transformer object: 可以通过在Transformer对象上设置特殊属性来启用此解决方法:

try {
    transformer.setOutputProperty("http://www.oracle.com/xml/is-standalone", "yes");
} catch (IllegalArgumentException e) {
    // Might be thrown by JDK versions not implementing the workaround.
} 

This way, the old behavior (printing a newline character after the XML declaration) is restored without adding the standalone attribute to the XML declaration. 这样,就可以恢复旧的行为(在XML声明之后打印换行符),而无需将独立属性添加到XML声明中。

For me writing the XML declaration to the Writer or OutputStream before writing the XML and telling the transformer to omit declaration was the only thing that worked. 对于我在写入XML之前将XML声明WriterOutputStream并告诉变换器省略声明是唯一有效的方法。 The only other solution to preserve the spacing appears to be VTD-XML library. 保留间距的唯一其他解决方案似乎是VTD-XML库。

    StringBuilder sb = new StringBuilder();
    sb.append("<?xml version=\"").append(doc.getXmlVersion()).append("\"");
    sb.append(" encoding=\"").append(doc.getXmlEncoding()).append("\"");
    if (doc.getXmlStandalone()) {
        sb.append(" standalone=\"").append("" + doc.getXmlStandalone()).append("\"");
    }
    sb.append("?>").append("\n");

    writer.write(sb.toString());
            TransformerFactory tf = TransformerFactory.newInstance();
    try {
        Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    }
    catch (Exception e)  {
    //snipped out for brevity
    }

This seems to be a problem (bug) of the XML implementaion in Java. 这似乎是Java中XML实现的一个问题(bug)。 The only way to get a linebreak after the XML declaration is to explicitly specify the standalone attribute. 在XML声明之后获取换行符的唯一方法是显式指定standalone属性。 You may set it to no , which is the implicit default, even if it is completely irrelevant when not using a DTD. 您可以将其设置为no ,这是隐式默认值,即使它在不使用DTD时完全不相关也是如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM