Notice: this Wiki will be going read only early in 2024 and edits will no longer be 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"
< EclipseLink | UserGuide | MOXy | Simple Values
(→Mapping enum constant name) |
|||
Line 14: | Line 14: | ||
==Mapping enum constant name== | ==Mapping enum constant name== | ||
+ | |||
+ | The following schema demonstrates an XML enumeration: | ||
+ | |||
+ | <source lang="xml"> | ||
+ | <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:simpleType> | ||
+ | </source> | ||
If your enums constant names themselves are sufficient for the XML representation, you can simply use the '''@XmlEnum''' annotation: | If your enums constant names themselves are sufficient for the XML representation, you can simply use the '''@XmlEnum''' annotation: | ||
<source lang="java"> | <source lang="java"> | ||
+ | @XmlAccessorType(XmlAccessType.FIELD) | ||
+ | public class Customer { | ||
+ | @XmlElement(name="first-name") | ||
+ | private String firstName; | ||
+ | |||
@XmlEnum | @XmlEnum | ||
− | public enum | + | public enum CustomerType { PROMO_CUSTOMER, NEW_CUSTOMER, VIP, NORMAL } |
</source> | </source> | ||
Line 32: | Line 49: | ||
<xs:enumeration value="SPADES"/> | <xs:enumeration value="SPADES"/> | ||
</xs:simpleType> | </xs:simpleType> | ||
− | </source> | + | </source> |
==Mapping enum constant name(value)== | ==Mapping enum constant name(value)== |
Revision as of 15:55, 14 January 2011
EclipseLink MOXy
EclipseLink | |
Website | |
Download | |
Community | |
Mailing List • Forums • IRC • mattermost | |
Issues | |
Open • Help Wanted • Bug Day | |
Contribute | |
Browse Source |
Key API
Mapping Enums
Java enums can be mapped to XML using the @XmlEnum and @XmlEnumValue annotations.
Mapping enum constant name
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:simpleType>
If your enums constant names themselves are sufficient for the XML representation, you can simply use the @XmlEnum annotation:
@XmlAccessorType(XmlAccessType.FIELD) public class Customer { @XmlElement(name="first-name") private String firstName; @XmlEnum public enum CustomerType { PROMO_CUSTOMER, NEW_CUSTOMER, VIP, NORMAL }
When marshalled to XML, an Person would look like this:
<xs:simpleType name="Card"> <xs:restriction base="xs:string"/> <xs:enumeration value="CLUBS"/> <xs:enumeration value="DIAMONDS"/> <xs:enumeration value="HEARTS"/> <xs:enumeration value="SPADES"/> </xs:simpleType>
Mapping enum constant name(value)
This code:
@XmlType @XmlEnum(Integer.class) public enum Coin { @XmlEnumValue("1") PENNY(1), @XmlEnumValue("5") NICKEL(5), @XmlEnumValue("10") DIME(10), @XmlEnumValue("25") QUARTER(25) }
will generate the following XML schema:
<xs:simpleType name="Coin"> <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>