|
|
(25 intermediate revisions by the same user not shown) |
Line 1: |
Line 1: |
− | {{EclipseLink_UserGuide
| + | Please see http://www.eclipse.org/eclipselink/documentation/2.4/moxy/simple_values004.htm |
− | |info=y
| + | |
− | |toc=y
| + | |
− | |eclipselink=y
| + | |
− | |eclipselinktype=MOXy
| + | |
− | |api=y
| + | |
− | |apis= * [http://www.eclipse.org/eclipselink/api/latest/index.html?javax/xml/bind/annotation/XmlEnumValue.html XmlEnumValue]
| + | |
− | * [http://www.eclipse.org/eclipselink/api/latest/index.html?javax/xml/bind/annotation/XmlEnum.html XmlEnum]
| + | |
− | }}
| + | |
− | | + | |
− | =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:
| + | |
− | | + | |
− | <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:
| + | |
− | | + | |
− | <source lang="java">
| + | |
− | @XmlRootElement
| + | |
− | @XmlAccessorType(XmlAccessType.FIELD)
| + | |
− | public class Customer {
| + | |
− | private CustomerType type = CustomerType.NEW_CUSTOMER;
| + | |
− | | + | |
− | @XmlEnum(String.class)
| + | |
− | public enum CustomerType { PROMO_CUSTOMER, NEW_CUSTOMER, VIP, NORMAL }
| + | |
− | | + | |
− | ...
| + | |
− | }
| + | |
− | </source>
| + | |
− | | + | |
− | When marshalled to XML, a '''Customer''' would look like this:
| + | |
− | | + | |
− | <source lang="xml">
| + | |
− | <customer>
| + | |
− | <type>NEW_CUSTOMER</type>
| + | |
− | </customer>
| + | |
− | </source>
| + | |
− | | + | |
− | ==Mapping enum constant name(value)==
| + | |
− | This code:
| + | |
− | | + | |
− | <source lang="java">
| + | |
− | @XmlType
| + | |
− | @XmlEnum(Integer.class)
| + | |
− | public enum Coin {
| + | |
− | @XmlEnumValue("1") PENNY(1),
| + | |
− | @XmlEnumValue("5") NICKEL(5),
| + | |
− | @XmlEnumValue("10") DIME(10),
| + | |
− | @XmlEnumValue("25") QUARTER(25) }
| + | |
− | </source>
| + | |
− | | + | |
− | will generate the following XML schema:
| + | |
− | | + | |
− | <source lang="xml">
| + | |
− | <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>
| + | |
− | </source>
| + | |
− | | + | |
− | {{EclipseLink_MOXy
| + | |
− | |previous= [[EclipseLink/UserGuide/MOXy/Simple_Values/Collections/XMLDirectCollectionMapping|Mapping collections of Simple Values]]
| + | |
− | |next= [[EclipseLink/UserGuide/MOXy/Simple_Values/Special_Schema_Types|Special Schema Types]]
| + | |
− | |up= [[EclipseLink/UserGuide/MOXy/Simple_Values|Simple Values]]
| + | |
− | |version=2.2.0 Draft
| + | |
− | }}
| + | |