一個JAXBElement
作爲模型的一部分,當一個JAXB(JSR-222)的實施將不能夠產生單獨根據價值告訴該做什麼。在您的例子,你可能不得不像一個元素:
<xsd:element
name="includeAllSubaccounts" type="xsd:boolean" nillable="true" minOccurs="0"/>
生成的屬性不能boolean
因爲boolean
不代表null
。你可以做財產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>
如果該方法需要一個['JAXBElement'(http://docs.oracle.com/javaee/5/api/javax/xml/bind/JAXBElement.html),爲什麼你想傳遞TRUE; ,布爾? – NullUserException
你可以在你有問題和堆棧跟蹤的地方加入代碼嗎? –
創建的JAXBElement,如:'JAXBElement的 jaxtrue =新的JAXBElement(QName的,Boolean.TYPE,Boolean.TRUE);'和傳遞 –
NullUserException