Skip to main content

JAXB Unmarshalling issue incase of Repeating elements

1 reply [Last post]
nagasreekrishna
Offline
Joined: 2011-02-21
Points: 0

Hi All
Iam using JAXB to unmarshall an xml file . But its not working as expected when there are repeating elements in the xml file . please find my xsd below.
<?xml version="1.0" encoding="ISO-8859-1" ?>
<xsd:schema
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:jxb="http://java.sun.com/xml/ns/jaxb" jxb:version="2.0">

<xsd:element name="name" type="xsd:string"/>
<xsd:element name="address" type="xsd:string"/>
<xsd:element name="city" type="xsd:string"/>
<xsd:element name="country" type="xsd:string"/>
<xsd:element name="title" type="xsd:string"/>
<xsd:element name="note" type="xsd:string"/>
<xsd:element name="quantity" type="xsd:positiveInteger"/>
<xsd:element name="price" type="xsd:decimal"/>

<xsd:complexType name="shipto.content">
<xsd:sequence>
<xsd:element ref="name"/>
<xsd:element ref="address"/>
<xsd:element ref="city" minOccurs="1" maxOccurs="1">
<xsd:annotation>
<xsd:appinfo>
<jxb:property name="city1"/>
</xsd:appinfo>
</xsd:annotation>
</xsd:element>

<xsd:element ref="country" minOccurs="1" maxOccurs="1">
<xsd:annotation>
<xsd:appinfo>
<jxb:property name="country1"/>
</xsd:appinfo>
</xsd:annotation>
</xsd:element>

<xsd:element ref="city" minOccurs="0">
<xsd:annotation>
<xsd:appinfo>
<jxb:property name="city2"/>
</xsd:appinfo>
</xsd:annotation>
</xsd:element>
<xsd:element ref="price"/>
<xsd:element ref="country" minOccurs="0" >
<xsd:annotation>
<xsd:appinfo>
<jxb:property name="country2"/>
</xsd:appinfo>
</xsd:annotation>
</xsd:element>
<xsd:element ref="quantity"/>
</xsd:sequence>
</xsd:complexType>
<xsd:element name="shipto" type="shipto.content"/>
</xsd:schema>
<<<XML input >>>>
<?xml version="1.0" encoding="UTF-8"?>
<shipto xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../xsd/Customer_withAnnotation.xsd">
<name>name</name>
<address>address</address>
<city>city1</city>
<country>country1</country>
<city>city2</city>
<price>10</price>
<country>country2</country>
<quantity>1</quantity>
</shipto>

Iam passing an xml file complaint to this xsd for unmarshalling .My java code is a simple unmarshalling nothing more than that . But after unmarshalling , if i try to get the value of 2nd repeating element it is coming as null . Can any one please help me .

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
kubatzki
Offline
Joined: 2011-03-25
Points: 0

dear nagasreekrishna,
There is actually a problem with your schema. the xsd:element name="shipto" type="shipto.content" allows only three elements. I recommend have a look at the examples in the jaxb reference implementation to get a feeling
how to design a suitable schema.