A JAXBElement
is generated as part of your model when a JAXB (JSR-222) implementation would not be able to tell what to do based on the value alone. In your example you probably had an element like:
<xsd:element
name="includeAllSubaccounts" type="xsd:boolean" nillable="true" minOccurs="0"/>
The generated property can't be boolean
because boolean
doesn't represent null
. You could make the property Boolean
but then how do you distinguish been a missing element and an element set with xsi:nil
. This is where JAXBElement comes in. See below for a full example:
Foo
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);
}
}
Demo
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);
}
}
Output
<?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>