Elemento JAXB del tipo enumeración


Así que sé cómo crear un tipo de enumeración, pero cuando establezco un tipo de elemento, el campo de elemento será de tipo string y no de tipo enum. ¿Cómo puedo crear una enumeración en mi esquema y hacer que JAXB la genere como un tipo de enumeración java?

Así es como estoy haciendo mi tipo de enumeración y creación de elementos:

<xsd:simpleType name="myEnum">
    <xsd:restriction base="xsd:string">
        <xsd:enumeration value="MY_ENUM_1"/>
        <xsd:enumeration value="MY_ENUM_2"/>
    </xsd:restriction>
</xsd:simpleType>

<xsd:element name="myEnumElement" type="ns1:myEnum"/>
Author: Dani, 2011-04-27

2 answers

Puede formar su esquema XML de la siguiente manera:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema targetNamespace="http://www.example.com" xmlns="http://www.example.com" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:simpleType name="myEnum">
        <xsd:restriction base="xsd:string">
            <xsd:enumeration value="MY_ENUM_1"/>
            <xsd:enumeration value="MY_ENUM_2"/>
        </xsd:restriction>
    </xsd:simpleType>
    <xsd:element name="root">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="local" type="myEnum"/>
                <xsd:element name="ref" type="myEnum"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>

Hará que se genere la siguiente enumeración:

package com.example;

import javax.xml.bind.annotation.*;

@XmlType(name = "myEnum")
@XmlEnum
public enum MyEnum {

    MY_ENUM_1,
    MY_ENUM_2;

    public String value() {
        return name();
    }

    public static MyEnum fromValue(String v) {
        return valueOf(v);
    }

}

Y la siguiente clase que aprovecha esa Enumeración:

package com.example;

import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "local",
    "ref"
})
@XmlRootElement(name = "root")
public class Root {

    @XmlElement(required = true)
    protected MyEnum local;
    @XmlElement(required = true)
    protected MyEnum ref;

    public MyEnum getLocal() {
        return local;
    }

    public void setLocal(MyEnum value) {
        this.local = value;
    }

    public MyEnum getRef() {
        return ref;
    }

    public void setRef(MyEnum value) {
        this.ref = value;
    }

}

Para Más Información

 43
Author: Blaise Doughan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-04-02 10:56:28

Véase jaxb:globalBindings/@typeSafeEnumBase aquí.

 3
Author: lexicore,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-05-03 22:00:08