Altering the XML header produced by the JAXB marshaller

java, jaxb

Solution

When marshalling to an `OutputStream` using a combination of the following produces the expected output.

    marshaller.setProperty("com.sun.xml.bind.xmlHeaders", "<?xml version=\"1.0\"?>");
    marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);

The problem you are seeing occurs when you marshal to a `Writer`, which appears to be a bug in the JAXB reference implementation. You can raise an issue at the link below:

- https://java.net/jira/browse/JAXB/

You could always do:

JAXBContext context;

try {
    context = JAXBContext.newInstance(heartbeat.getClass());
    StringWriter writer = new StringWriter();
    writer.append("<?xml version=\"1.0\"?>");
    Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);

    heartbeat.setHeader(header);
    heartbeat.setHeartbeatEvent(event);

    marshaller.marshal(heartbeat, writer);
    String stringXML = writer.toString();
    return stringXML;

} catch (JAXBException e) {
    throw new RuntimeException("Problems generating XML in specified "
            + "encoding, underlying problem is " + e.getMessage(),
            e);
}

EclipseLink JAXB (MOXy) also supports the `com.sun.xml.bind.xmlHeaders` and it works correctly when marshalling to a `Writer` (I'm the MOXy lead)

- http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html

Problem

I am currently using the following code to marshal an object into an xml string ``` JAXBContext context; try { context = JAXBContext.newInstance(heartbeat.getClass()); StringWriter writer = new StringWriter(); Marshaller marshaller = context.createMarshaller(); heartbeat.setHeader(header); heartbeat.setHeartbeatEvent(event); marshaller.marshal(heartbeat, writer); String stringXML = writer.toString(); return stringXML; } catch (JAXBException e) { throw new RuntimeException("Problems generating XML in specified " + "encoding, underlying problem is " + e.getMessage(), e); } ``` Which produces the following header ``` <?xml version="1.0" encoding="UTF-8" standalone="yes"?> ``` My desired output is the following ``` <?xml version=\"1.0\"?> ``` By adding this to the marshaller ``` marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.FALSE); marshaller.setProperty("com.sun.xml.bind.xmlHeaders", "<?xml version=\"1.0\"?>"); ``` I receive ``` <?xml version="1.0" encoding="UTF-8" standalone="yes"?><?xml version="1.0"?> ``` and changing the JAXB_FRAGMENT property to TRUE removes the header entirely. I have been following the JAXB - Remove 'standalone="yes"' from generated XML thread attempting to solve the problem but I have had no luck so far. Can someone please give me some insight on how to get my desired header from the JAXB marshaller?

Original source

Related problems