JAXB: Unmarshalling does not always populate certain classes?

Posted by user278458 on Stack Overflow See other posts from Stack Overflow or by user278458
Published on 2010-02-22T05:15:41Z Indexed on 2010/03/27 18:03 UTC
Read the original article Hit count: 301

Filed under:
|
|

Hello,

I have a JAXB class generation problem I was hoping to get some help with. Here's the part of the XML that is the source of my problem...

Code:

<xs:complexType name="IDType"> 
<xs:choice minOccurs="0" maxOccurs="2"> 
  <xs:element name="DriversLicense"    minOccurs="0" maxOccurs="1" type="an..35" /> 
    <xs:element name="SSN"        minOccurs="0" maxOccurs="1" type="an..35" /> 
    <xs:element name="CompanyID"       minOccurs="0" maxOccurs="1" type="an..35" /> 
  </xs:choice> 
</xs:complexType> 
<xs:simpleType name="an..35"> 
  <xs:restriction base="an"> 
    <xs:maxLength value="35" /> 
  </xs:restriction> 
</xs:simpleType> 

<xs:simpleType name="an"> 
   <xs:restriction base="xs:string"> 
     <xs:pattern value="[ !-~]*" /> 
   </xs:restriction> 
</xs:simpleType>

...now this will generate JAXBElement types due the the "choice" with a "maxOccurs > 1" . I want to avoid those, so I did that by modifying the code to use a "Wrapper" element and move the maxOccurs up to a sequence tag as follows...

Code: 
<xs:complexType name="IDType"> 
<xs:sequence maxOccurs="2"> 
  <xs:element name=Wrapper>
  <xs:complexType>
  <xs:choice> 
  <xs:element name="DriversLicense"    minOccurs="0" maxOccurs="1" type="an..35" /> 
    <xs:element name="SSN"        minOccurs="0" maxOccurs="1" type="an..35" /> 
    <xs:element name="CompanyID"       minOccurs="0" maxOccurs="1" type="an..35" /> 
  </xs:choice>
  </xs:complexType>
  </xs:element>
</xs:sequence> 
</xs:complexType>

<xs:simpleType name="an..35"> 
  <xs:restriction base="an"> 
    <xs:maxLength value="35" /> 
  </xs:restriction> 
</xs:simpleType> 

<xs:simpleType name="an"> 
   <xs:restriction base="xs:string"> 
     <xs:pattern value="[ !-~]*" /> 
   </xs:restriction> 
</xs:simpleType> 

For class generating, looks like it works great - the JAXB element is replaced with a list of wrappers as String (i.e. List ) and compiles fine.

However, when I unmarshall the actual XML data into the generated classes the data in the wrapper class is not populated - yet JAXB does not throw an exception.

My question is: Do I need to change the schema a different way to make this work? Or is there something I can add/change/delete to the generated code or annotations?

Appreciate any help you can offer!

Thanks.

© Stack Overflow or respective owner

Related posts about jaxb

Related posts about xml-schema