Jump to: navigation, search

Difference between revisions of "EclipseLink/UserGuide/MOXy/Simple Values/Enums"

(Mapping enum constant name)
Line 10: Line 10:
  
 
=Mapping Enums=
 
=Mapping Enums=
Java enums can be mapped to XML using the '''@XmlEnum''' and '''@XmlEnumValue''' annotations.
+
Java '''enum'''s can be mapped to XML using the '''@XmlEnum''' and '''@XmlEnumValue''' annotations.
  
  
Line 27: Line 27:
 
</source>
 
</source>
  
If your enums constant names themselves are sufficient for the XML representation, you can simply use the '''@XmlEnum''' annotation:
+
If your '''enum''' constant names themselves are sufficient for the XML representation, you can simply use the '''@XmlEnum''' annotation:
  
 
<source lang="java">
 
<source lang="java">
Line 50: Line 50:
 
</source>
 
</source>
  
==Mapping enum constant name(value)==
+
 
This code:
+
==Mapping enum with Values==
 +
 
 +
For non-String enumerations, such as:
 +
 
 +
<source lang="xml">
 +
<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>
 +
</source>
 +
 
 +
The '''@XmlEnumValue''' annotation is used to provide an XML value for each enum constant:
  
 
<source lang="java">
 
<source lang="java">
    @XmlType
+
@XmlRootElement
    @XmlEnum(Integer.class)
+
@XmlAccessorType(XmlAccessType.FIELD)
    public enum Coin {  
+
public class Customer {
        @XmlEnumValue("1") PENNY(1),
+
 
        @XmlEnumValue("5") NICKEL(5),
+
  private RewardLevel discount = RewardLevel.NONE;
        @XmlEnumValue("10") DIME(10),
+
 
        @XmlEnumValue("25") QUARTER(25) }
+
  @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);
 +
 
 +
      ...
 +
}
 
</source>
 
</source>
  
will generate the following XML schema:
+
When marshalled to XML, a '''Customer''' would look like this:
  
 
<source lang="xml">
 
<source lang="xml">
    <xs:simpleType name="Coin">
+
<customer>
      <xs:restriction base="xs:int">
+
  <discount>0</discount>
        <xs:enumeration value="1"/>
+
</customer>
        <xs:enumeration value="5"/>
+
        <xs:enumeration value="10"/>
+
        <xs:enumeration value="25"/>
+
      </xs:restriction>
+
    </xs:simpleType>
+
 
</source>
 
</source>
 +
  
 
{{EclipseLink_MOXy
 
{{EclipseLink_MOXy

Revision as of 16:39, 14 January 2011

EclipseLink MOXy

link="http://wiki.eclipse.org/EclipseLink"
EclipseLink
Website
Download
Community
Mailing ListForumsIRC
Bugzilla
Open
Help Wanted
Bug Day
Contribute
Browse Source

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 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 }
 
   ...
}

When marshalled to XML, a Customer would look like this:

<customer>
   <type>NEW_CUSTOMER</type>
</customer>


Mapping enum with Values

For non-String enumerations, such as:

<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);
 
      ...
}

When marshalled to XML, a Customer would look like this:

<customer>
   <discount>0</discount>
</customer>


Eclipselink-logo.gif
Version: 2.2.0 Draft
Other versions...