2013-08-26 10 views
6

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?

risposta

13

Quando il marshalling su un OutputStream utilizzando una combinazione di quanto segue produce l'output previsto.

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

Il problema che state vedendo si verifica quando si effettua il marshalling a un Writer, che sembra essere un bug nell'implementazione di riferimento JAXB. È possibile sollevare un problema al link qui sotto:


Si può sempre fare:

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) supporta anche il com.sun.xml.bind.xmlHeaders e funziona correttamente quando si esegue il marshalling su un Writer (Io sono in vantaggio Moxy)

+2

ottengo un 'PropertyException' quando si imposta la proprietà' sun' . –

+0

Su MOXy? Che versione stai usando? –

+0

No MOxy. Presuppone che la proprietà sia stata utilizzata con l'implementazione Java. –

7

Questo ha funzionato per me

marshaller.setProperty("com.sun.xml.internal.bind.xmlHeaders", "<?xml version=\"1.0\" encoding=\"UTF-8\"?>");

+3

"com.sun.xml.internal.bind.xmlHeaders" funziona quando si utilizza jaxb da jdk 1.8 (almeno su Windows con java 1.8.0_60) – dosw

Problemi correlati