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); }
What creates the following heading
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
My desired result is as follows
<?xml version=\"1.0\"?>
Adding this to the Marshaller
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.FALSE); marshaller.setProperty("com.sun.xml.bind.xmlHeaders", "<?xml version=\"1.0\"?>");
I get
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><?xml version="1.0"?>
and changing the JAXB_FRAGMENT property to TRUE completely removes the header. I am following JAXB - Remove 'standalone = "yes"' from the generated XML stream trying to solve the problem, but still no luck. Can someone please give me some idea on how to get the desired header from a JAXB marshaller?
source share