2

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?

Daniel Widdis
  • 8,424
  • 13
  • 41
  • 63
Massimo
  • 23
  • 2

1 Answers1

0

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

bdoughan
  • 147,609
  • 23
  • 300
  • 400
  • This is only true if you don't use already the option to avoid JaxElement type generation for some attributes. In that case the option choiceContentProperty="true" it is ignored. To avoid this problem I had to change the schema as this: so the elements when null are not marshaled with xsi:nil. But somethimes to change the schema it's not an option. – Massimo May 12 '11 at 07:17