Notice: This Wiki is now read only and edits are no longer possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.
Difference between revisions of "EclipseLink/UserGuide/MOXy/Simple Values/Enums"
(→Mapping Enums to Custom XML Values) |
|||
Line 117: | Line 117: | ||
<xml-enums> | <xml-enums> | ||
<xml-enum java-enum="CustomerType" value="java.lang.String"> | <xml-enum java-enum="CustomerType" value="java.lang.String"> | ||
− | <xml-enum-value java-enum-value=" | + | <xml-enum-value java-enum-value="NONE">0</xml-enum-value> |
− | <xml-enum-value java-enum-value=" | + | <xml-enum-value java-enum-value="BRONZE">5</xml-enum-value> |
− | <xml-enum-value java-enum-value=" | + | <xml-enum-value java-enum-value="SILVER">10</xml-enum-value> |
− | <xml-enum-value java-enum-value=" | + | <xml-enum-value java-enum-value="GOLD">25</xml-enum-value> |
+ | <xml-enum-value java-enum-value="PLATINUM">40</xml-enum-value> | ||
</xml-enum> | </xml-enum> | ||
</xml-enums> | </xml-enums> | ||
Line 128: | Line 129: | ||
<xml-root-element name="customer"/> | <xml-root-element name="customer"/> | ||
<java-attributes> | <java-attributes> | ||
− | <xml-element java-attribute=" | + | <xml-element java-attribute="discount" type="RewardLevel"/> |
</java-attributes> | </java-attributes> | ||
</java-type> | </java-type> |
Revision as of 12:11, 19 January 2011
EclipseLink MOXy
EclipseLink | |
Website | |
Download | |
Community | |
Mailing List • Forums • IRC • mattermost | |
Issues | |
Open • Help Wanted • Bug Day | |
Contribute | |
Browse Source |
Key API
Contents
Mapping Enums
Java enums can be mapped to XML using the @XmlEnum and @XmlEnumValue annotations.
Mapping Enums using Constant Names
The following schema demonstrates an XML enumeration:
<xs:simpleType name="CustomerType"> <xs:restriction base="xs:string"/> <xs:enumeration value="PROMO_CUSTOMER"/> <xs:enumeration value="NEW_CUSTOMER"/> <xs:enumeration value="VIP"/> <xs:enumeration value="NORMAL"/> </xs:restriction> </xs:simpleType>
If your enum constant names themselves are sufficient for the XML representation, you can simply use the @XmlEnum annotation:
@XmlRootElement @XmlAccessorType(XmlAccessType.FIELD) public class Customer { private CustomerType type = CustomerType.NEW_CUSTOMER; @XmlEnum(String.class) private enum CustomerType { PROMO_CUSTOMER, NEW_CUSTOMER, VIP, NORMAL } ... }
The following example shows how to define this mapping in EclipseLink OXM Metadata:
... <xml-enums> <xml-enum java-enum="CustomerType" value="java.lang.String"> <xml-enum-value java-enum-value="PROMO_CUSTOMER">PROMO_CUSTOMER</xml-enum-value> <xml-enum-value java-enum-value="NEW_CUSTOMER">NEW_CUSTOMER</xml-enum-value> <xml-enum-value java-enum-value="VIP">VIP</xml-enum-value> <xml-enum-value java-enum-value="NORMAL">NORMAL</xml-enum-value> </xml-enum> </xml-enums> <java-types> <java-type name="Customer"> <xml-root-element name="customer"/> <java-attributes> <xml-element java-attribute="type" type="CustomerType"/> </java-attributes> </java-type> </java-types> ...
When marshalled to XML, a Customer would look like this:
<customer> <type>NEW_CUSTOMER</type> </customer>
Mapping Enums to Custom XML Values
Given the following XML schema enumeration:
<xs:simpleType name="RewardLevel"> <xs:restriction base="xs:int"> <xs:enumeration value="1"/> <xs:enumeration value="5"/> <xs:enumeration value="10"/> <xs:enumeration value="25"/> </xs:restriction> </xs:simpleType>
The @XmlEnumValue annotation is used to provide an XML value for each enum constant:
@XmlRootElement @XmlAccessorType(XmlAccessType.FIELD) public class Customer { private RewardLevel discount = RewardLevel.NONE; @XmlEnum(Integer.class) private enum RewardLevel { @XmlEnumValue("0") NONE(0), @XmlEnumValue("5") BRONZE(5), @XmlEnumValue("10") SILVER(10), @XmlEnumValue("25") GOLD(25), @XmlEnumValue("40") PLATINUM(40); ... }
The following example shows how to define this mapping in EclipseLink OXM Metadata:
... <xml-enums> <xml-enum java-enum="CustomerType" value="java.lang.String"> <xml-enum-value java-enum-value="NONE">0</xml-enum-value> <xml-enum-value java-enum-value="BRONZE">5</xml-enum-value> <xml-enum-value java-enum-value="SILVER">10</xml-enum-value> <xml-enum-value java-enum-value="GOLD">25</xml-enum-value> <xml-enum-value java-enum-value="PLATINUM">40</xml-enum-value> </xml-enum> </xml-enums> <java-types> <java-type name="Customer"> <xml-root-element name="customer"/> <java-attributes> <xml-element java-attribute="discount" type="RewardLevel"/> </java-attributes> </java-type> </java-types> ...
When marshalled to XML, a Customer would look like this:
<customer> <discount>0</discount> </customer>