4
votes

Using JAXB 2.2.4 I am generating Java code with the following binding file:

<bindings xmlns="http://java.sun.com/xml/ns/jaxb" version="2.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema">

<globalBindings generateElementProperty="false">

</globalBindings>

Unfortunately, the generated code has a nillable = true annotation. See the following example:

@XmlElement(name = "ArtID", nillable = true)
protected STEBeitrag artID;

This is the definition of STEBeitrag:

<xsd:complexType name="CT_Beitrag">
    <xsd:complexContent>
        <xsd:extension base="allgemein:CT_Objekt">
            <xsd:sequence>
                <xsd:element name="ArtID" type="daten:STE_Beitrag" minOccurs="0" maxOccurs="1" nillable="true"/></xsd:element>
            </xsd:sequence>
        </xsd:extension>
     </xsd:complexContent>
</xsd:complexType>

<xsd:complexType name="STE_Beitrag" abstract="true">
    <xsd:simpleContent>
     <xsd:extension base="xsd:string"/>
   </xsd:simpleContent>
</xsd:complexType>

If I do not set the ArtID in the generated CT_Beitrag object then the unmarshaller produces an output like

<ns:ArtID xsi:nil="true"/>

The element ArtID has an abstract type and therefore this XML output is invalid.

How can I generate code with JAXB that omits the nillable=true in the XmlElement annotation? By the way, the schema canĀ“t be changed.

2

2 Answers

1
votes

I have no solution, but this problem is addressed in bug http://java.net/jira/browse/JAXB-890 I hope this problem wil be solved.

0
votes

I don't how to do what you're asking and I'd be surprised if what you're asking is worth the effort.

There are 2 options that jump to my mind:

  1. Change the schema (I know you said you can't but perhaps you can take a local copy of the schema if you can't change it due to it being hosted on a server outside of your control)
  2. Change the generated code... simply change nillable=true to nillable=false