एक JAXBElement
अपने मॉडल का हिस्सा है जब एक JAXB (JSR-222) कार्यान्वयन के लिए सक्षम नहीं होगा के रूप में उत्पन्न होता है बताएं कि अकेले मूल्य के आधार पर क्या करना है। उत्पन्न संपत्ति boolean
क्योंकि boolean
null
प्रतिनिधित्व नहीं करता है नहीं किया जा सकता
<xsd:element
name="includeAllSubaccounts" type="xsd:boolean" nillable="true" minOccurs="0"/>
: अपने उदाहरण में आप शायद की तरह एक तत्व था। आप संपत्ति Boolean
बना सकते हैं लेकिन फिर आप एक गायब तत्व और xsi:nil
के साथ एक तत्व सेट कैसे अंतर करते हैं। यह जहां JAXBElement में आता है एक पूर्ण उदाहरण के लिए नीचे देखें:।
फू
package forum12713373;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.*;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Foo {
@XmlElementRef(name="absent")
JAXBElement<Boolean> absent;
@XmlElementRef(name="setToNull")
JAXBElement<Boolean> setToNull;
@XmlElementRef(name="setToValue")
JAXBElement<Boolean> setToValue;
}
ObjectFactory
package forum12713373;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.*;
import javax.xml.namespace.QName;
@XmlRegistry
public class ObjectFactory {
@XmlElementDecl(name="absent")
public JAXBElement<Boolean> createAbsent(Boolean value) {
return new JAXBElement(new QName("absent"), Boolean.class, value);
}
@XmlElementDecl(name="setToNull")
public JAXBElement<Boolean> createSetToNull(Boolean value) {
return new JAXBElement(new QName("setToNull"), Boolean.class, value);
}
@XmlElementDecl(name="setToValue")
public JAXBElement<Boolean> createSetToValue(Boolean value) {
return new JAXBElement(new QName("setToValue"), Boolean.class, value);
}
}
डेमो
package forum12713373;
import javax.xml.bind.*;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Foo.class);
ObjectFactory objectFactory = new ObjectFactory();
Foo foo = new Foo();
foo.absent = null;
foo.setToNull = objectFactory.createSetToNull(null);
foo.setToValue = objectFactory.createSetToValue(false);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(foo, System.out);
}
}
आउटपुट
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<foo>
<setToNull xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
<setToValue>false</setToValue>
</foo>
स्रोत
2012-10-03 19:13:28
यदि विधि ['JAXBElement'] (http://docs.oracle.com/javaee/5/api/javax/xml/bind/JAXBElement.html) की अपेक्षा करती है, तो आप 'सत्य' को पारित करने का प्रयास क्यों कर रहे हैं , एक बूलियन? – NullUserException
क्या आप उस कोड को शामिल कर सकते हैं जहां आपको समस्या हो रही है और स्टैकट्रैक? –
एक JAXBElement बनाएं, उदाहरण के लिए: 'JAXBElement jaxtrue = new jaxBElement (qname, Boolean.TYPE, Boolean.TRUE); और –
NullUserException