开发者

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

Is 开发者_StackOverflow社区there any way to tell the Transformer (when serializing an XML document using DOM), to omit the standalone attribute?

Preferably without using a hack, i.e. ommitting the whole XML declaration and then prepending it manually.

My current code:

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();

Current:

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

Intended:

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


Figured it out..

Instead of changes to the transformer,

I add the following to the document object.

  document.setXmlStandalone(true);


document.setXmlStandalone(true/false); is working OK.


You have to use a combination of:

doc.setXmlStandalone(true);

and

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


Which Java version are you using and/or which XSLT transformer? With Sun Java 1.6.0_16, the standalone attribute is only set in the output document if you set the output property and the content is also correct.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜