background image

HashMap Schema

<< Interface XmlAdapter | Running the XmlAdapter Field Example >>
<< Interface XmlAdapter | Running the XmlAdapter Field Example >>

HashMap Schema

<xs:element name=
"entry" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<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 data type 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, the example uses 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>
Class AdapterPurchaseListToHashMap implements the XmlAdapter interface. In class
KitchenWorldBasket
, the @XmlJavaTypeAdapter annotation is used to pair
Java-to-Schema Examples
The Java EE 5 Tutorial · September 2007
542