background image

The XML schema generated

<< The XmlAdapter interface | j2s-xmlAttribute-field Example >>
<< The XmlAdapter interface | j2s-xmlAttribute-field Example >>
J
2
S
-
XML
A
DAPTER
-
FIELD
E
XAMPLE
71
<xs:element name="key" minOccurs="0"
type="xs:anyType"/>
<xs:element name="value" minOccurs="0"
type="xs:anyType"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
In the default HashMap schema, key and value are both elements and are of
datatype
anyType
. The XML content will look like this:
<basket>
<entry>
<key>9027</>
<value>glasstop stove in black</>
</entry>
<entry>
<key>288</>
<value>wooden spoon</>
</entry>
</basket>
To resolve this issue, we wrote two Java classes,
PurchaseList
and
PartEntry
,
that reflect the needed schema format for unmarshalling/marshalling the content.
The XML schema generated for these classes is as follows:
<xs:complexType name="PurchaseListType">
<xs:sequence>
<xs:element name="entry" type="partEntry"
nillable="true" maxOccurs="unbounded"
minOccurs="0"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="partEntry">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="key" type="xs:int"
use="required"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>