此内容没有您所选择的语言版本。

34.3. Enumerations


Overview

In XML Schema, enumerated types are simple types that are defined using the xsd:enumeration facet. Unlike atomic simple types, they are mapped to Java enums.

Defining an enumerated type in XML Schema

Enumerations are a simple type using the xsd:enumeration facet. Each xsd:enumeration facet defines one possible value for the enumerated type.
Example 34.5, “XML Schema Defined Enumeration” shows the definition for an enumerated type. It has the following possible values:
  • big
  • large
  • mungo
  • gargantuan

Example 34.5. XML Schema Defined Enumeration

<simpleType name="widgetSize">
  <restriction base="xsd:string">
    <enumeration value="big"/>
    <enumeration value="large"/>
    <enumeration value="mungo"/>
    <enumeration value="gargantuan"/>
  </restriction>
Copy to Clipboard Toggle word wrap

Mapping to Java

XML Schema enumerations where the base type is xsd:string are automatically mapped to Java enum type. You can instruct the code generator to map enumerations with other base types to Java enum types by using the customizations described in Section 38.4, “Customizing Enumeration Mapping”.
The enum type is created as follows:
  1. The name of the type is taken from the name attribute of the simple type definition and converted to a Java identifier.
    In general, this means converting the first character of the XML Schema's name to an uppercase letter. If the first character of the XML Schema's name is an invalid character, an underscrore (_) is prepended to the name.
  2. For each enumeration facet, an enum constant is generated based on the value of the value attribute.
    The constant's name is derived by converting all of the lowercase letters in the value to their uppercase equivalent.
  3. A constructor is generated that takes the Java type mapped from the enumeration's base type.
  4. A public method called value() is generated to access the facet value that is represented by an instance of the type.
    The return type of the value() method is the base type of the XML Schema type.
  5. A public method called fromValue() is generated to create an instance of the enum type based on a facet value.
    The parameter type of the value() method is the base type of the XML Schema type.
  6. The class is decorated with the @XmlEnum annotation.

Example 34.6. Generated Enumerated Type for a String Bases XML Schema Enumeration

@XmlType(name = "widgetSize")
@XmlEnum
public enum WidgetSize {

    @XmlEnumValue("big")
    BIG("big"),
    @XmlEnumValue("large")
    LARGE("large"),
    @XmlEnumValue("mungo")
    MUNGO("mungo"),
    @XmlEnumValue("gargantuan")
    GARGANTUAN("gargantuan");
    private final String value;

    WidgetSize(String v) {
        value = v;
    }

    public String value() {
        return value;
    }

    public static WidgetSize fromValue(String v) {
        for (WidgetSize c: WidgetSize.values()) {
            if (c.value.equals(v)) {
                return c;
            }
        }
        throw new IllegalArgumentException(v);
    }

}
Copy to Clipboard Toggle word wrap
返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2025 Red Hat