[xml] Remove 'standalone="yes"' from generated XML

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"?>

This question is related to xml xsd jaxb xml-binding

The answer is


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.


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

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.


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


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\"?>");

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.


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

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.


in JAXB that is part of JDK1.6

marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);

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

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


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.


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

It works for me on Java 8


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


in JAXB that is part of JDK1.6

marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);

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


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

It works for me on Java 8


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


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.


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\"?>");

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);  

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.


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


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\"?>");

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);  

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\"?>");

Examples related to xml

strange error in my Animation Drawable How do I POST XML data to a webservice with Postman? PHP XML Extension: Not installed How to add a Hint in spinner in XML Generating Request/Response XML from a WSDL Manifest Merger failed with multiple errors in Android Studio How to set menu to Toolbar in Android How to add colored border on cardview? Android: ScrollView vs NestedScrollView WARNING: Exception encountered during context initialization - cancelling refresh attempt

Examples related to xsd

How to generate xsd from wsdl How to reference a local XML Schema file correctly? XML Schema Validation : Cannot find the declaration of element Using Notepad++ to validate XML against an XSD Error in spring application context schema cvc-elt.1: Cannot find the declaration of element 'MyElement' Importing xsd into wsdl SoapUI "failed to load url" error when loading WSDL How to make an element in XML schema optional? XML Schema How to Restrict Attribute by Enumeration

Examples related to jaxb

Java 11 package javax.xml.bind does not exist Java: How to resolve java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException Convert Java object to XML string no suitable HttpMessageConverter found for response type javax.xml.bind.UnmarshalException: unexpected element. Expected elements are (none) java.lang.VerifyError: Expecting a stackmap frame at branch target JDK 1.7 javax.xml.bind.JAXBException: Class *** nor any of its super class is known to this context How to generate JAXB classes from XSD? convert xml to java object using jaxb (unmarshal) I can't understand why this JAXB IllegalAnnotationException is thrown

Examples related to xml-binding

Remove 'standalone="yes"' from generated XML