繁体   English   中英

使用Java DOM + Transformer时省略xml声明中的独立属性。

[英]Omitting the standalone attribute in xml declaration when using Java DOM + Transformer.

有没有办法告诉Transformer(使用DOM序列化XML文档时),省略独立属性?

最好不使用hack,即省略整个XML声明,然后手动预先设置。

我目前的代码:

Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.STANDALONE, "yes"); //Note nothing is changed

StreamResult result = new StreamResult(new StringWriter());
DOMSource source = new DOMSource(document);
transformer.transform(source, result);
 return result.getWriter().toString();

当前:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<someElement/>

意:

<?xml version="1.0" encoding="UTF-8">
<someElement/>

弄清楚了..

而不是改变变压器,

我将以下内容添加到文档对象中。

  document.setXmlStandalone(true);

document.setXmlStandalone(true/false); 工作正常。

你必须使用以下组合:

doc.setXmlStandalone(true);

transformer.setOutputProperty(OutputKeys.STANDALONE, "yes"); // this is used to show the standalone tag

您使用的是哪个Java版本和/或哪个XSLT转换器? 对于Sun Java 1.6.0_16,如果设置输出属性且内容也正确,则仅在输出文档中设置独立属性。

暂无
暂无

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

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