Remove 'standalone=“yes”' from generated X

2019-01-08 05:54发布

Do you know of a JAXB setting to prevent standalone="yes" from being generated in the resulting XML?

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

9条回答
Juvenile、少年°
2楼-- · 2019-01-08 06:02

in JAXB that is part of JDK1.6

marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
查看更多
Anthone
3楼-- · 2019-01-08 06:08

You can use: marshaller.setProperty("jaxb.fragment", Boolean.TRUE);

It works for me on Java 8

查看更多
看我几分像从前
4楼-- · 2019-01-08 06:12
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.

查看更多
Melony?
5楼-- · 2019-01-08 06:12

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

查看更多
Deceive 欺骗
6楼-- · 2019-01-08 06:12

I don't have a high enough "reputation" to have the "privilege" to comment. ;-)

@Debasis, note that the property you've specified:

"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

查看更多
再贱就再见
7楼-- · 2019-01-08 06:13

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 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.

查看更多
登录 后发表回答