Do you know of a JAXB setting to prevent standalone="yes" from being generated in the resulting XML?
您知道JAXB设置防止在生成的XML中生成独立=“yes”吗?
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
9 个解决方案
#1
52
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.
但是我不认为这是最好的做法。
#2
108
in JAXB that is part of JDK1.6
在JAXB中,这是JDK1.6的一部分
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
#3
53
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,从而避免独立的=“yes”属性。
#4
4
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(例如使用命名实体),那么它将停止独立,因此在XML声明中不允许独立=“yes”。
However standalone XML can be used anywhere, while non-standalone is problematic for XML parsers that don't load externals.
然而,独立的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的软件进行互操作之外,我不认为这个声明会成为一个问题,而是看到了一些可怕的regex soup。
#5
3
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 String
at the beginning of your output stream / method
要删除所有XML声明,只需在输出流/方法的开头编写自己的字符串
#6
2
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有效。独立=\“no\”可以删除,只获取xml部分的其余部分
#7
2
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。可以将封送器的JAXB_FRAGMENT选项设置为“true”(省略了默认的xml处理指令),并使用XMLStreamWriter的writeprocessinginstructions方法插入您自己的:
xmlStreamWriter.writeProcessingInstruction("xml", "version=\"1.0\" encoding=\"UTF-8\"");
jaxbMarshaller.setProperty( Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
jaxbMarshaller.marshal(object, xmlStreamWriter);
xmlStreamWriter.writeEndDocument();
#8
1
You can use: marshaller.setProperty("jaxb.fragment", Boolean.TRUE);
可以使用:marshaller.setProperty(“jaxb.fragment”,Boolean.TRUE);
It works for me on Java 8
它适用于Java 8
#9
0
I don't have a high enough "reputation" to have the "privilege" to comment. ;-)
我没有足够高的“声誉”来发表评论。:-)
@Debasis, note that the property you've specified:
@Debasis,注意您指定的属性:
"com.sun.xml.internal.bind.xmlHeaders"
should be:
应该是:
"com.sun.xml.bind.xmlHeaders" (without the "internal", which are not meant to be used by the public)
If I use the "internal" property as you did, I get a javax.xml.bind.PropertyException
如果我像您那样使用“内部”属性,就会得到javax.xml.bin . propertyexception
#1
52
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.
但是我不认为这是最好的做法。
#2
108
in JAXB that is part of JDK1.6
在JAXB中,这是JDK1.6的一部分
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
#3
53
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,从而避免独立的=“yes”属性。
#4
4
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(例如使用命名实体),那么它将停止独立,因此在XML声明中不允许独立=“yes”。
However standalone XML can be used anywhere, while non-standalone is problematic for XML parsers that don't load externals.
然而,独立的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的软件进行互操作之外,我不认为这个声明会成为一个问题,而是看到了一些可怕的regex soup。
#5
3
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 String
at the beginning of your output stream / method
要删除所有XML声明,只需在输出流/方法的开头编写自己的字符串
#6
2
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有效。独立=\“no\”可以删除,只获取xml部分的其余部分
#7
2
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。可以将封送器的JAXB_FRAGMENT选项设置为“true”(省略了默认的xml处理指令),并使用XMLStreamWriter的writeprocessinginstructions方法插入您自己的:
xmlStreamWriter.writeProcessingInstruction("xml", "version=\"1.0\" encoding=\"UTF-8\"");
jaxbMarshaller.setProperty( Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
jaxbMarshaller.marshal(object, xmlStreamWriter);
xmlStreamWriter.writeEndDocument();
#8
1
You can use: marshaller.setProperty("jaxb.fragment", Boolean.TRUE);
可以使用:marshaller.setProperty(“jaxb.fragment”,Boolean.TRUE);
It works for me on Java 8
它适用于Java 8
#9
0
I don't have a high enough "reputation" to have the "privilege" to comment. ;-)
我没有足够高的“声誉”来发表评论。:-)
@Debasis, note that the property you've specified:
@Debasis,注意您指定的属性:
"com.sun.xml.internal.bind.xmlHeaders"
should be:
应该是:
"com.sun.xml.bind.xmlHeaders" (without the "internal", which are not meant to be used by the public)
If I use the "internal" property as you did, I get a javax.xml.bind.PropertyException
如果我像您那样使用“内部”属性,就会得到javax.xml.bin . propertyexception