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?