background image

j2s-xmlAttribute-field Example

<< The XML schema generated | j2s-xmlRootElement Example >>
<< The XML schema generated | j2s-xmlRootElement Example >>
72
U
SING
JAXB
Class
AdapterPurchaseListToHashMap
implements the
XmlAdapter
interface.
In class KitchenWorldBasket, the @XmlJavaTypeAdapter annotation is used to
pair
AdapterPurchaseListToHashMap
with field HashMap
basket
. This pairing
will cause the marshal/unmarshal method of
AdapterPurchaseListToHashMap
to be called for any corresponding marshal/unmarshal action on
KitchenWorld-
Basket
.
j2s-xmlAttribute-field Example
The j2s-xmlAttribute-field example shows how to use the @XmlAttribute anno-
tation to define a property or field to be treated as an XML attribute.
The @XmlAttribute annotation maps a field or JavaBean property to an XML
attribute. The following rules are imposed:
· A static final field is mapped to a XML fixed attribute.
· When the field or property is a collection type, the items of the collection
type must map to a schema simple type.
· When the field or property is other than a collection type, the type must
map to a schema simple type.
When following the JavaBean programming paradigm, a property is defined by a
"get" and "set" prefix on a field name.
int zip;
public int getZip(){return zip;}
public void setZip(int z){zip=z;}
Within a bean class, you have the choice of setting the @XmlAttribute annota-
tion on one of three components: the field, the setter method, or the getter
method. If you set the @XmlAttribute annotation on the field, the setter method
will need to be renamed or there will be a naming conflict at compile time. If you
set the @XmlAttribute annotationt on one of the methods, it must be set on
either the setter or getter method, but not on both.
The j2s-xmlAttribute-field example shows how to use the @XmlAttribute anno-
tationd on a static final field, on a field rather than on one of the corresponding
bean methods, on a bean property (method), and on a field that is other than a
collection type.
In class
USAddress
, fields, country, and zip are tagged as
attributes. The
setZip
method was disabled to avoid the compile error. Prop-
erty state was tagged as an attribute on the setter method. You could have used
the getter method instead. In class
PurchaseOrderType
, field
cCardVendor
is a