2013-08-26 13 views
6

मैं वर्तमान में एक xml स्ट्रिंगXml शीर्ष JAXB marshaller द्वारा उत्पादित

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); 
    } 

निम्नलिखित में से कौन हैडर

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 

मेरे वांछित आउटपुट है पैदा करता है में एक वस्तु मार्शल करने के लिए निम्नलिखित कोड का उपयोग कर रहा फेरबदल निम्नलिखित

<?xml version=\"1.0\"?> 

marshaller को यह जोड़ कर

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

मैं

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><?xml version="1.0"?> 

प्राप्त करते हैं और सही पर JAXB_FRAGMENT संपत्ति को बदलने हैडर पूरी तरह से निकाल देता है। मैं JAXB - Remove 'standalone="yes"' from generated XML धागे को हल करने का प्रयास कर रहा हूं, लेकिन अब तक मुझे कोई भाग्य नहीं मिला है। क्या कोई मुझे जेएक्सबी मार्शलर से अपना वांछित हेडर प्राप्त करने के बारे में कुछ अंतर्दृष्टि दे सकता है?

उत्तर

13

जब निम्नलिखित के संयोजन का उपयोग करके OutputStream पर marshalling अपेक्षित आउटपुट उत्पन्न करता है।

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

समस्या आप देख रहे हैं तब होता है जब आप एक Writer, जो JAXB संदर्भ कार्यान्वयन में एक बग प्रतीत होता है करने के लिए मार्शल। आप नीचे दिए गए लिंक पर एक मुद्दे को उठाने कर सकते हैं:


तुम हमेशा कर सकता है:

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) भी com.sun.xml.bind.xmlHeaders का समर्थन करता है और इसे सही ढंग से काम करता है जब Writer पर marshalling (मैं MOXY नेतृत्व कर रहा हूँ)

+2

मैं एक 'PropertyException' जब' sun' संपत्ति की स्थापना । –

+0

मोक्सी पर? आपके द्वारा कौन सा संस्करण उपयोग किया जा रहा है? –

+0

कोई मोक्सी नहीं। मैंने माना कि संपत्ति जावा कार्यान्वयन के साथ प्रयोग किया गया था। –

7

यह मेरे लिए काम किया

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

+3

"com.sun.xml.internal.bind.xml हैडर" jdk 1.8 से jaxb का उपयोग करते समय काम करता है (कम से कम जावा 1.8.0_60 के साथ विंडोज़ पर) – dosw

संबंधित मुद्दे