Remove 'standalone="yes"' from generated XML
Asked Answered
D

15

86

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"?>
Ditmore answered 10/11, 2008 at 14:32 Comment(2)
Why is that a problem?Bedspread
@Bedspread in my project that to get used with Tibco and WebMethod, its will generate error because standalone=yes is not recognizedSusi
N
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.

Nasturtium answered 9/12, 2008 at 8:26 Comment(6)
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.Dissent
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.Oppression
Doesn't work with JAXB in JDK1.6. See so_mv's answer for correct solution.Moneybag
That explodes in flight : exception.U
@Moneybag : you can try property class : "com.sun.xml.internal.bind.xmlHeaders"Hodgkins
@baybora.oren "com.sun.xml.internal.bind.xmlHeaders" fails as well on Java 1.7.0_51 with a javax.xml.bind.PropertyException.Poynter
W
124

in JAXB that is part of JDK1.6

marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
Willettawillette answered 1/11, 2010 at 8:27 Comment(3)
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.U
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 under the Supported Properties section.Lilylilyan
doesn't work on java 11. the whole fragment <?xml version="1.0" encoding="UTF-8" standalone="yes"?> goes awayLatashialatch
N
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.

Nasturtium answered 9/12, 2008 at 8:26 Comment(6)
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.Dissent
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.Oppression
Doesn't work with JAXB in JDK1.6. See so_mv's answer for correct solution.Moneybag
That explodes in flight : exception.U
@Moneybag : you can try property class : "com.sun.xml.internal.bind.xmlHeaders"Hodgkins
@baybora.oren "com.sun.xml.internal.bind.xmlHeaders" fails as well on Java 1.7.0_51 with a javax.xml.bind.PropertyException.Poynter
P
67

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.

Placatory answered 25/3, 2011 at 11:0 Comment(6)
The "setProperty" with "xmlDeclaration" explodes in flight : exception.U
The "setProperty" line with "xmlHeaders" fails too. PropertyException. So this is not a solution.U
which version of java and jaxb are you using?Placatory
@Placatory The setProperty with xmlHeaders command does not work with jdk1.6.0_24 and JAXB version 2.1Waylin
Regardless of java6 or java7 I had to add 'internal', ie marshaller.setProperty("com.sun.xml.internal.bind.xmlHeaders", "<?xml version=\"1.0\" encoding=\"UTF-8\"?>");Impurity
doesn't work on java 11. the whole fragment <?xml version="1.0" encoding="UTF-8" standalone="yes"?> goes awayLatashialatch
P
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

Peltast answered 7/11, 2016 at 12:13 Comment(0)
B
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.

Bedspread answered 24/12, 2008 at 10:51 Comment(3)
Exactly, any allegedly xml-processing system that barfs on xml declaration seems highly suspicious.Oppression
In what way does it answer the question?Kosher
How does one make the document depend on DOCTYPE?Maple
H
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

Hooch answered 10/8, 2016 at 11:40 Comment(1)
error : javax.xml.bind.PropertyException: name: com.sun.xml.internal.bind.xmlHeaders value: <?xml version="1.0" encoding="UTF-8"?>Susi
H
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();
Henrie answered 3/5, 2017 at 9:55 Comment(0)
S
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();
  }
Staminody answered 6/8, 2019 at 18:56 Comment(1)
Cool, work on Java 11Latashialatch
J
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\"?>");
Jovi answered 27/11, 2019 at 1:44 Comment(0)
M
2

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

It works for me on Java 8

Motorcycle answered 10/4, 2018 at 17:46 Comment(0)
S
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

Simferopol answered 21/9, 2016 at 23:34 Comment(0)
E
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);  
Exchangeable answered 10/2, 2019 at 19:33 Comment(0)
B
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\"?>");
Baziotes answered 20/3, 2020 at 12:28 Comment(0)
C
0

I was getting PropertyException when using:

marshaller.setProperty("com.sun.xml.internal.bind.xmlHeaders", "<?xml version=\"1.0\" encoding=\"UTF-8\"?>");

Instead what helped me was setting JAXB_FRAGMENT to true and then writing the custom header using:

marshaller.setProperty("org.glassfish.jaxb.xmlHeaders", "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE note SYSTEM>"
Curtin answered 5/3 at 7:54 Comment(0)
P
0

Here's an updated answer for Java 17 / Java 21 (using the Eclipse Jakarta reference implementation of JAXB).

My dependencies in POM look like this (for a Java SE application):

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.glassfish.jaxb</groupId>
                <artifactId>jaxb-bom</artifactId>
                <version>4.0.5</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <!-- JAXB API -->
        <dependency>
            <groupId>jakarta.xml.bind</groupId>
            <artifactId>jakarta.xml.bind-api</artifactId>
        </dependency>

        <!-- JAXB Implementation (needed when not executing in a Java EE server) -->
        <dependency>
            <groupId>org.glassfish.jaxb</groupId>
            <artifactId>jaxb-runtime</artifactId>
            <scope>runtime</scope>
        </dependency>
    </dependencies>

The relevant property is nowadays named org.glassfish.jaxb.xmlHeaders as per the documentation found HERE.

So, you would do:

String customXmlHeader = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
marshaller.setProperty("org.glassfish.jaxb.xmlHeaders", customXmlHeader);
Puma answered 2/4 at 9:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.