从生成的 XML 中删除 'standalone="yes"'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/277996/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove 'standalone="yes"' from generated XML
提问by Johan Pelgrim
Do you know of a JAXB setting to prevent standalone="yes"from being generated in the resulting XML?
您是否知道防止在结果 XML 中生成standalone="yes"的 JAXB 设置?
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
采纳答案by Sam
marshaller.setProperty("com.sun.xml.bind.xmlDeclaration", Boolean.FALSE);
can be used to have no
可以用来没有
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
However i wouldnt consider this best practice.
但是我不会考虑这个最佳实践。
回答by so_mv
in JAXB that is part of JDK1.6
在属于 JDK1.6 的 JAXB 中
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
回答by WarFox
You can either use
您可以使用
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
or
或者
marshaller.setProperty("com.sun.xml.bind.xmlDeclaration", Boolean.FALSE)
to disable the default XML declaration, and then add your custom XML declaration,
禁用默认的 XML 声明,然后添加您的自定义 XML 声明,
<?xml version="1.0" encoding="UTF-8"?>
by
经过
marshaller.setProperty("com.sun.xml.bind.xmlHeaders",
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
to the generated xml, thus avoiding the standalone="yes"property.
到生成的 xml,从而避免了standalone="yes"属性。
回答by benez
just if someone else is still struggeling with this problem, you may consider using
只是如果其他人仍在努力解决这个问题,您可以考虑使用
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
to remove all of the XML declaration and just write your own Stringat the beginning of your output stream / method
删除所有 XML 声明,并String在输出流/方法的开头编写自己的声明
回答by Kornel
If you make document dependent on DOCTYPE(e.g. use named entities) then it will stop being standalone, thus standalone="yes"won't be allowed in XML declaration.
如果您使文档依赖于DOCTYPE(例如使用命名实体),那么它将不再是独立的,因此standalone="yes"在 XML 声明中将不被允许。
However standalone XML can be used anywhere, while non-standalone is problematic for XML parsers that don't load externals.
然而,独立的 XML 可以在任何地方使用,而非独立的 XML 解析器对于不加载外部的 XML 解析器是有问题的。
I don't see how this declaration could be a problem, other than for interoperability with software that doesn't support XML, but some horrible regex soup.
除了与不支持 XML 但一些可怕的正则表达式汤的软件的互操作性之外,我看不出这个声明有什么问题。
回答by Debasis Das
jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
jaxbMarshaller.setProperty("com.sun.xml.internal.bind.xmlHeaders", "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>");
This worked for me with JDK1.7. standalone=\"no\" can be removed to get only rest of the xml part
这对我来说适用于 JDK1.7。可以删除 standalone=\"no\" 以只获取 xml 部分的其余部分
回答by eddo
If you are using only the default javax.xml package, you could set the JAXB_FRAGMENT option of the marshaller to 'true' (this omits the default xml processing instruction) and use the writeProcessingInstruction method of the XMLStreamWriter to insert your own:
如果您只使用默认的 javax.xml 包,您可以将编组器的 JAXB_FRAGMENT 选项设置为“true”(这省略了默认的 xml 处理指令)并使用 XMLStreamWriter 的 writeProcessingInstruction 方法插入您自己的:
xmlStreamWriter.writeProcessingInstruction("xml", "version=\"1.0\" encoding=\"UTF-8\"");
jaxbMarshaller.setProperty( Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
jaxbMarshaller.marshal(object, xmlStreamWriter);
xmlStreamWriter.writeEndDocument();
回答by William Funchal Pereira
You can use: marshaller.setProperty("jaxb.fragment", Boolean.TRUE);
您可以使用: marshaller.setProperty("jaxb.fragment", Boolean.TRUE);
It works for me on Java 8
它在 Java 8 上对我有用
回答by Alisha Setia
In case you are getting property exception, add the following configuration:
如果您遇到属性异常,请添加以下配置:
jaxbMarshaller.setProperty("com.sun.xml.internal.bind.xmlHeaders",
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
jaxbMarshaller.setProperty("com.sun.xml.internal.bind.xmlDeclaration", Boolean.FALSE);
jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
回答by Bernardo Mello
just try
你试一试
private String marshaling2(Object object) throws JAXBException, XMLStreamException {
JAXBContext jaxbContext = JAXBContext.newInstance(object.getClass());
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
StringWriter writer = new StringWriter();
writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
jaxbMarshaller.marshal(object, writer);
return writer.toString();
}

