Attualmente sto usando il seguente codice per il marshalling di un oggetto in una stringa XMLAlterare l'intestazione XML prodotto dal marshaller JAXB
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);
}
che produce la seguente intestazione
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
mio output desiderato è il seguente
<?xml version=\"1.0\"?>
aggiungendo questo al marshaller
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.FALSE);
marshaller.setProperty("com.sun.xml.bind.xmlHeaders", "<?xml version=\"1.0\"?>");
ricevo
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><?xml version="1.0"?>
e modificando la proprietà JAXB_FRAGMENT TRUE rimuove l'intestazione del tutto. Ho seguito il thread JAXB - Remove 'standalone="yes"' from generated XML tentando di risolvere il problema, ma finora non ho avuto fortuna. Qualcuno può darmi qualche informazione su come ottenere l'intestazione desiderata dal marshaller JAXB?
ottengo un 'PropertyException' quando si imposta la proprietà' sun' . –
Su MOXy? Che versione stai usando? –
No MOxy. Presuppone che la proprietà sia stata utilizzata con l'implementazione Java. –