2012-10-27 15 views
6
<a> 
    <b1>b1</b1> 
    <b2>b2</b2> 
    <b3> 
     <c1></c1> 
     <c2></c2> 
    </b3> 
    <b3> 
     <c1></c1> 
     <c2></c2> 
    </b3> 
    <b3> 
     <c1></c1> 
     <c2></c2> 
    </b3> 
</a> 

बिना दोहराया XmlElement के बाद से, <b3s> कहना सब <b3> एक आवरण तत्व में शामिल नहीं हैं, जब मैं जैक्सन XmlMapper का उपयोग अपने POJO जावा बीन वर्ग के लिए एक्सएमएल फ़ाइल को पढ़ने के लिए, मैं अपवादJAXB Unmarshaller: आवरण

मिला
com.fasterxml.jackson.databind.JsonMappingException: Can not instantiate value of type [simple type, class com.xxxxx] from String value; no single-String constructor/factory method (through reference chain: com.xxxx["xxx"]->com.xxx["xxx"]) 

मैं किस एनोटेशन का उपयोग करूं?

@XmlElement 
public List<B3> b3; 

उत्तर

0

जैक्सन कैसे B3 का एक उदाहरण में एक स्ट्रिंग चालू करने के लिए पता नहीं है। एक कन्स्ट्रक्टर (या फैक्ट्री विधि जो B3 लौटाता है) B3 पर जोड़ें जो एक String लेता है। निर्माता उदाहरण: अगर JAXB एनोटेशन का उपयोग

@JacksonXmlElementWrapper(useWrapping=false) 

वैकल्पिक:

class B3 { 
    // ... 
    public B3(String value) { 
     // do something with value 
    } 
    // ... 
} 

http://wiki.fasterxml.com/JacksonFeatureCreators

4

आप "unwrapped" प्रतिनिधित्व का उपयोग करना चाहते हैं, तो आप जैक्सन 2.1 उपयोग करने की आवश्यकता है, और unwrapped विकल्प का संकेत डिफ़ॉल्ट रूप से रैपिंग का उपयोग नहीं करना चाहिए।

अंत में, आप भी डिफ़ॉल्ट नहीं आवरण तत्व का उपयोग करने के साथ बदल सकते हैं:

JacksonXmlModule module = new JacksonXmlModule(); 
// to default to using "unwrapped" Lists: 
module.setDefaultUseWrapper(false); 
XmlMapper xmlMapper = new XmlMapper(module); 
5

नोट

जैक्सन एक JAXB (JSR-222) अनुरूप क्रियान्वयन नहीं है। इसका मतलब यह है वहाँ यह कैसे मानक JAXB की व्याख्या पर कोई गारंटी एनोटेशन


डिफ़ॉल्ट रूप से एक JAXB (JSR-222) अनुरूप कार्यान्वयन संग्रह गुण के लिए एक आवरण तत्व लागू नहीं होता है।

एक

डिफ़ॉल्ट रूप से एक JAXB (JSR-222) कार्यान्वयन गुणों के आधार पर मैपिंग लागू हो जाएगी। अंतरिक्ष को बचाने के लिए मैंने उन विधियों को छोड़ दिया है और @XmlAccessorType(XmlAccessType.FIELD) निर्दिष्ट किया है ताकि मेटाडेटा आवृत्ति चर (फ़ील्ड) से प्राप्त किया जा सके।

package forum13097559; 

import java.util.List; 
import javax.xml.bind.annotation.*; 

@XmlRootElement 
@XmlAccessorType(XmlAccessType.FIELD) 
public class A { 

    private String b1; 
    private String b2; 
    private List<B3> b3; 

} 

बी 3

package forum13097559; 

import javax.xml.bind.annotation.*; 

@XmlAccessorType(XmlAccessType.FIELD) 
public class B3 { 

    private String c1; 
    private String c2; 

} 

डेमो

package forum13097559; 

import java.io.File; 
import javax.xml.bind.*; 

public class Demo { 

    public static void main(String[] args) throws Exception { 
     JAXBContext jc = JAXBContext.newInstance(A.class); 

     Unmarshaller unmarshaller = jc.createUnmarshaller(); 
     File xml = new File("src/forum13097559/input.xml"); 
     A a = (A) unmarshaller.unmarshal(xml); 

     Marshaller marshaller = jc.createMarshaller(); 
     marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 
     marshaller.marshal(a, System.out); 
    } 

} 

इनपुट।एक्सएमएल/आउटपुट

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<a> 
    <b1>b1</b1> 
    <b2>b2</b2> 
    <b3> 
     <c1></c1> 
     <c2></c2> 
    </b3> 
    <b3> 
     <c1></c1> 
     <c2></c2> 
    </b3> 
    <b3> 
     <c1></c1> 
     <c2></c2> 
    </b3> 
</a> 

अधिक जानकारी के लिए