有没有办法告诉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/>
发布于 2009-11-06 04:03:36
想明白了..。
而不是对变压器进行更改,
我将以下内容添加到document对象。
document.setXmlStandalone(true);
发布于 2012-01-18 10:21:06
document.setXmlStandalone(true/false);
工作正常。
发布于 2018-10-26 00:51:18
您必须使用以下各项的组合:
doc.setXmlStandalone(true);
和
transformer.setOutputProperty(OutputKeys.STANDALONE, "yes"); // this is used to show the standalone tag
https://stackoverflow.com/questions/1682796
复制相似问题