87

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"?>
Johan Pelgrim
  • 6,033
  • 6
  • 37
  • 46

13 Answers13

124

in JAXB that is part of JDK1.6

marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
Lii
  • 11,553
  • 8
  • 64
  • 88
so_mv
  • 3,939
  • 5
  • 29
  • 40
  • 17
    This does not give the expected result. This removes all the XML declaration line. What I want is just removing the `standalone` attribute in the XML declaration. – Nicolas Barbulesco Apr 24 '13 at 09:11
  • 1
    If you wonder why this isn't working for you, just like I did, then the answer is that the effect depends on which marshal api you are using. For marshal(Object,Outputstream) and marshal(Object,Writer) this works as suggested here. For marshal(Object, Node) it has no effect. For the remaining marshal api implications have a look [here](https://jaxb.java.net/nonav/2.2.4/docs/api/javax/xml/bind/Marshaller.html) under the Supported Properties section. – Lasse Samson Jan 22 '15 at 14:50
  • doesn't work on java 11. the whole fragment goes away – Andriy Feb 10 '23 at 16:43
68

This property:

marshaller.setProperty("com.sun.xml.bind.xmlDeclaration", false);

...can be used to have no:

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

However, I wouldn't consider this best practice.

Lii
  • 11,553
  • 8
  • 64
  • 88
Sam
  • 6,240
  • 4
  • 42
  • 53
  • 2
    Thanks, this is exactly what I needed. I would agree it is best practice to include the line, but a web service I am interfacing with does not expect it. – jgrowl Mar 17 '10 at 15:29
  • 6
    Good that it works, but FWIW, service is broken if it can not accept legal xml, so it's probably good to file a bug report against it. – StaxMan Jan 08 '11 at 08:05
  • 4
    Doesn't work with JAXB in JDK1.6. See so_mv's answer for correct solution. – sversch Aug 14 '12 at 07:32
  • 1
    That explodes in flight : exception. – Nicolas Barbulesco Apr 24 '13 at 09:38
  • 1
    @sversch : you can try property class : "com.sun.xml.internal.bind.xmlHeaders" – bora.oren Aug 19 '13 at 12:27
  • 1
    @baybora.oren "com.sun.xml.internal.bind.xmlHeaders" fails as well on Java 1.7.0_51 with a javax.xml.bind.PropertyException. – Phil Dec 05 '14 at 02:52
68

You can either use

marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);

or

marshaller.setProperty("com.sun.xml.bind.xmlDeclaration", 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.

Lii
  • 11,553
  • 8
  • 64
  • 88
WarFox
  • 4,933
  • 3
  • 28
  • 32
7

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

benez
  • 1,856
  • 22
  • 28
6

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.

However standalone XML can be used anywhere, while non-standalone is problematic for XML parsers that don't load externals.

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.

Kornel
  • 97,764
  • 37
  • 219
  • 309
5
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

Debasis Das
  • 61
  • 1
  • 2
  • 1
    error : javax.xml.bind.PropertyException: name: com.sun.xml.internal.bind.xmlHeaders value: – aswzen Jul 26 '19 at 09:00
5

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:

xmlStreamWriter.writeProcessingInstruction("xml", "version=\"1.0\" encoding=\"UTF-8\"");
jaxbMarshaller.setProperty( Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
jaxbMarshaller.marshal(object, xmlStreamWriter);
xmlStreamWriter.writeEndDocument();
eddo
  • 71
  • 1
  • 3
3

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

I'm using Java 1.8 and JAXB 2.3.1

First, be sure to be using java 1.8 in pom.xml

<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>

Then in source code I used: (the key was the internal part)

// remove standalone=yes
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
marshaller.setProperty("com.sun.xml.internal.bind.xmlHeaders", "<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
Cesar
  • 31
  • 2
2

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

It works for me on Java 8

1

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

Ari
  • 61
  • 4
1

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);  
JoSSte
  • 2,953
  • 6
  • 34
  • 54
1

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

but want this: <?xml version="1.0" encoding="UTF-8"?>

Just do:

marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
marshaller.setProperty("com.sun.xml.internal.bind.xmlHeaders", "<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
Baked Inhalf
  • 3,375
  • 1
  • 31
  • 45