I am using JAXB to marshal an object, which produces the following output
<?xml version="1.0"?><Jetstream xmlns="http://Jetstream.TersoSolutions.com/v1.0/HeartbeatEvent">......
My desired output is
<?xml version="1.0"?><Jetstream xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://Jetstream.TersoSolutions.com/v1.0/HeartbeatEvent">
I am marshaling the object using the following code
JAXBContext context;
try {
context = JAXBContext.newInstance(heartbeat.getClass());
OutputStream writer = new ByteArrayOutputStream();
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
marshaller.setProperty("com.sun.xml.bind.xmlHeaders", "<?xml version=\"1.0\"?>");
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);
Adding
marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, "http://www.w3.org/2001/XMLSchema");
will cause output to be close to expected, except in the wrong order and with the wrong prefix
<?xml version="1.0"?><Jetstream xmlns="http://Jetstream.TersoSolutions.com/v1.0/HeartbeatEvent" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3.org/2001/XMLSchema">...
I would like to ask for some assistance with generating my namespace in the proper order and with the correct prefixes.
EDIT
After following the advice in the comment I still haven't had any luck. I added the following to my package-info.java file in the relevant package.
@javax.xml.bind.annotation.XmlSchema(
namespace = "http://Jetstream.TersoSolutions.com/v1.0/HeartbeatEvent",
elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED,
xmlns={@XmlNs(prefix="xsd", namespaceURI="http://www.w3.org/2001/XMLSchema") })
and my resulting output is
<?xml version="1.0"?><ns2:Jetstream xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ns2="http://Jetstream.TersoSolutions.com/v1.0/HeartbeatEvent">
for some reason the prefix was cut short, and now there are ns2: tags throughout the xml. These tags cannot be present.