@XmlElement (nillable = true)
You just need to specify @XmlElement(nillable=true) in your field / property to get the following:
@XmlElement(nillable=true) private String foo;
Creating from an XML Schema
Below I will demonstrate how to generate this mapping if you look at the XML schema.
XML Schema (schema.xsd)
<?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="foo"> <xs:complexType> <xs:sequence> <xs:element name="myStringElementName" type="xs:string" nillable="true" minOccurs="0" /> <xs:element name="myIntElementName" type="xs:int" nillable="true" minOccurs="0" /> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>
Why do you get a property of type JAXBElement
A JAXBElement type property is created in your model because you have an element that has nillable, this is minOccurs="0" . Using JAXBElement allows the model to distinguish between the missing element (the property is null) and the presence of an element with nil="true" (JAXBElement with the set nil flag set).
<xs:element name="myStringElementName" type="xs:string" nillable="true" minOccurs="0" />
External Binding File (binding.xml)
An external binding file can be specified to tell the JAXB implementation not to generate any JAXBElement type JAXBElement . Please note that this will not allow JAXB to crawl all XML documents.
<?xml version="1.0" encoding="UTF-8"?> <jaxb:bindings version="2.0" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"> <jaxb:bindings> <jaxb:globalBindings generateElementProperty="false"/> </jaxb:bindings> </jaxb:bindings>
XJC call
Below is an example of how to use an external bind file from an XJC call
xjc -b binding.xml schema.xsd
Generated Model (Foo)
The generated model will look something like this:
import javax.xml.bind.annotation.*; @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "", propOrder = { "myStringElementName", "myIntElementName" }) @XmlRootElement(name = "foo") public class Foo { @XmlElement(nillable = true) protected String myStringElementName; @XmlElement(nillable = true) protected Integer myIntElementName; public String getMyStringElementName() { return myStringElementName; } public void setMyStringElementName(String value) { this.myStringElementName = value; } public Integer getMyIntElementName() { return myIntElementName; } public void setMyIntElementName(Integer value) { this.myIntElementName = value; } }
Additional Information
Blaise donough
source share