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"

m
m (Replacing page with 'Please see http://www.eclipse.org/eclipselink/documentation/2.4/moxy/simple_values004.htm')
 
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/XmlEnum.html XmlEnum]
+
* [http://www.eclipse.org/eclipselink/api/latest/index.html?javax/xml/bind/annotation/XmlEnumValue.html XmlEnumValue]
+
}}
+
 
+
=Mapping Enums=
+
Java '''enum'''s can be mapped to XML using the '''@XmlEnum''' and '''@XmlEnumValue''' annotations.
+
 
+
 
+
==Mapping Enums using Constant Names==
+
 
+
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:restriction>
+
</xs:simpleType>
+
</source>
+
 
+
If your '''enum''' constant names themselves are sufficient for the XML representation, you can simply use the '''@XmlEnum''' annotation:
+
 
+
<source lang="java">
+
package example;
+
 
+
import javax.xml.bind.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 }
+
 
+
  ...
+
}
+
</source>
+
 
+
The following example shows how to define this mapping in EclipseLink OXM Metadata:
+
 
+
<source lang="xml">
+
...
+
<xml-enums>
+
  <xml-enum java-enum="CustomerType" value="java.lang.String">
+
      <xml-enum-value java-enum-value="PROMO_CUSTOMER">PROMO_CUSTOMER</xml-enum-value>
+
      <xml-enum-value java-enum-value="NEW_CUSTOMER">NEW_CUSTOMER</xml-enum-value>
+
      <xml-enum-value java-enum-value="VIP">VIP</xml-enum-value>
+
      <xml-enum-value java-enum-value="NORMAL">NORMAL</xml-enum-value>
+
  </xml-enum>
+
</xml-enums>
+
 
+
<java-types>
+
  <java-type name="Customer">
+
      <xml-root-element name="customer"/>
+
      <java-attributes>
+
        <xml-element java-attribute="type" type="CustomerType"/>
+
      </java-attributes>
+
  </java-type>
+
</java-types>
+
...
+
</source>
+
 
+
When marshalled to XML, a '''Customer''' would look like this:
+
 
+
<source lang="xml">
+
<customer>
+
  <type>NEW_CUSTOMER</type>
+
</customer>
+
</source>
+
 
+
==Mapping Enums to Custom XML Values==
+
 
+
Given the following XML schema enumeration:
+
 
+
<source lang="xml">
+
<xs:simpleType name="CustomerType">
+
  <xs:restriction base="xs:int">
+
      <xs:enumeration value="1"/>
+
      <xs:enumeration value="2"/>
+
      <xs:enumeration value="3"/>
+
      <xs:enumeration value="4"/>
+
  </xs:restriction>
+
</xs:simpleType>
+
</source>
+
 
+
To model this in Java, the '''@XmlEnumValue''' annotation is used to provide an XML value for each enum constant:
+
 
+
<source lang="java">
+
package example;
+
 
+
import javax.xml.bind.annotation.*;
+
 
+
@XmlRootElement
+
@XmlAccessorType(XmlAccessType.FIELD)
+
public class Customer {
+
 
+
  private CustomerType type = CustomerType.NEW_CUSTOMER;
+
 
+
  @XmlEnum(Integer.class)
+
  private enum CustomerType {
+
      @XmlEnumValue("1") PROMO_CUSTOMER,
+
      @XmlEnumValue("2") NEW_CUSTOMER,
+
      @XmlEnumValue("3") VIP,
+
      @XmlEnumValue("4") NORMAL;
+
 
+
      ...
+
}
+
</source>
+
 
+
The following example shows how to define this mapping in EclipseLink OXM Metadata:
+
 
+
<source lang="xml">
+
...
+
<xml-enums>
+
  <xml-enum java-enum="CustomerType" value="java.lang.Integer">
+
      <xml-enum-value java-enum-value="PROMO_CUSTOMER">1</xml-enum-value>
+
      <xml-enum-value java-enum-value="NEW_CUSTOMER">2</xml-enum-value>
+
      <xml-enum-value java-enum-value="VIP">3</xml-enum-value>
+
      <xml-enum-value java-enum-value="NORMAL">4</xml-enum-value>
+
  </xml-enum>
+
</xml-enums>
+
 
+
<java-types>
+
  <java-type name="Customer">
+
      <xml-root-element name="customer"/>
+
      <java-attributes>
+
        <xml-element java-attribute="type" type="CustomerType"/>
+
      </java-attributes>
+
  </java-type>
+
</java-types>
+
...
+
</source>
+
 
+
When marshalled to XML, a new '''Customer''' would look like this:
+
 
+
<source lang="xml">
+
<customer>
+
  <type>2</type>
+
</customer>
+
</source>
+
 
+
 
+
 
+
{{EclipseLink_MOXy
+
|previous=[[EclipseLink/UserGuide/MOXy/Simple Values/Multiple Mappings|Multiple Mappings for a Single Property ]]
+
|next=    [[EclipseLink/UserGuide/MOXy/Simple_Values/Special_Schema_Types|Special Schema Types]]
+
}}
+

Latest revision as of 10:18, 8 November 2012

Please see http://www.eclipse.org/eclipselink/documentation/2.4/moxy/simple_values004.htm

Back to the top