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

From Eclipsepedia

Jump to: navigation, search
Line 15: Line 15:
 
==Mapping enum constant name==
 
==Mapping enum constant name==
  
If your enums constant names
+
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">
Line 22: Line 22:
 
</source>
 
</source>
  
will generate the following XML schema:
+
When marshalled to XML, an Person would look like this:
  
 
<source lang="xml">
 
<source lang="xml">
    <xs:simpleType name="Card">
+
<xs:simpleType name="Card">
      <xs:restriction base="xs:string"/>
+
  <xs:restriction base="xs:string"/>
        <xs:enumeration value="CLUBS"/>
+
      <xs:enumeration value="CLUBS"/>
        <xs:enumeration value="DIAMONDS"/>
+
      <xs:enumeration value="DIAMONDS"/>
        <xs:enumeration value="HEARTS"/>
+
      <xs:enumeration value="HEARTS"/>
        <xs:enumeration value="SPADES"/>
+
      <xs:enumeration value="SPADES"/>
    </xs:simpleType>
+
</xs:simpleType>
 
</source>  
 
</source>  
  

Revision as of 15:48, 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

Contents


Mapping Enums

Java enums can be mapped to XML using the @XmlEnum and @XmlEnumValue annotations.


Mapping enum constant name

If your enums constant names themselves are sufficient for the XML representation, you can simply use the @XmlEnum annotation:

@XmlEnum
public enum Card { CLUBS, DIAMONDS, HEARTS, SPADES }

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>

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