Skip to main content

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.

Jump to: navigation, search

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

(Mapping enum constant name)
(Mapping enum constant name)
Line 20: Line 20:
 
<xs:simpleType name="CustomerType">
 
<xs:simpleType name="CustomerType">
 
   <xs:restriction base="xs:string"/>
 
   <xs:restriction base="xs:string"/>
      <xs:enumeration value="PROMO_CUSTOMER"/>
+
  <xs:enumeration value="PROMO_CUSTOMER"/>
      <xs:enumeration value="NEW_CUSTOMER"/>
+
  <xs:enumeration value="NEW_CUSTOMER"/>
      <xs:enumeration value="VIP"/>
+
  <xs:enumeration value="VIP"/>
      <xs:enumeration value="NORMAL"/>
+
  <xs:enumeration value="NORMAL"/>
 
</xs:simpleType>
 
</xs:simpleType>
 
</source>
 
</source>
Line 30: Line 30:
  
 
<source lang="java">
 
<source lang="java">
 +
@XmlRootElement
 
@XmlAccessorType(XmlAccessType.FIELD)
 
@XmlAccessorType(XmlAccessType.FIELD)
 
public class Customer {
 
public class Customer {
  @XmlElement(name="type")
 
 
   private CustomerType type = CustomerType.NEW_CUSTOMER;
 
   private CustomerType type = CustomerType.NEW_CUSTOMER;
  
  ...
+
   @XmlEnum(String.class)
 
+
   @XmlEnum
+
 
   public enum CustomerType { PROMO_CUSTOMER, NEW_CUSTOMER, VIP, NORMAL }
 
   public enum CustomerType { PROMO_CUSTOMER, NEW_CUSTOMER, VIP, NORMAL }
  
 +
  ...
 
}
 
}
 
@XmlEnum
 
public enum CustomerType { PROMO_CUSTOMER, NEW_CUSTOMER, VIP, NORMAL }
 
 
</source>
 
</source>
  
When marshalled to XML, an Person would look like this:
+
When marshalled to XML, a '''Customer''' would look like this:
  
 
<source lang="xml">
 
<source lang="xml">
<xs:simpleType name="Card">
+
<customer>
   <xs:restriction base="xs:string"/>
+
   <type>NEW_CUSTOMER</type>
      <xs:enumeration value="CLUBS"/>
+
</customer>
      <xs:enumeration value="DIAMONDS"/>
+
      <xs:enumeration value="HEARTS"/>
+
      <xs:enumeration value="SPADES"/>
+
</xs:simpleType>
+
 
</source>
 
</source>
  

Revision as of 16:06, 14 January 2011

EclipseLink MOXy

Eclipselink-logo.gif
EclipseLink
Website
Download
Community
Mailing ListForumsIRCmattermost
Issues
OpenHelp WantedBug 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 enums 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)
   public 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 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...

Back to the top