Search code examples
nulljaxbeclipselinkmarshalling

JAXB EclipseLink problem with marshalling CHOICE with nillable elements


I have a schema defined as this:

<complexType name="x">
    <sequence>
        <element name="year" type="date"/>
            <choice>
                <element name="comuneNascita" type="string" nillable="true"/>
                <element name="statoNascita" type="string" nillable="true"/>
             </choice>
    </sequence>
</complexType>

When I try to marshall the class generated with xjc ( with xjc:simple option ) and I get this result:

[...]
<statoNascita xsi:nil="true"/>
<comuneNascita>xxx</comuneNascita>
[...]

Removing nillable="true" solve this problem but then I have to specify a valid element ( not nilled ).

Any workaround?


Solution

  • You can avoid your problem by having a property annotated as follows:

    @XmlElements({
       @XmlElement(name="comuneNascita", type=String.class),
       @XmlElement(name="statoNascita", type=String.class),
    })
    

    You can get XJC to generate a property annotated as above using a JAXB bindings file:

    <?xml version="1.0" encoding="UTF-8"?>
    <bindings xmlns="http://java.sun.com/xml/ns/jaxb"
              version="2.1">
        <globalBindings choiceContentProperty="true"/>
    </bindings> 
    

    For More Information