JAXB - Annotations, Annotations for Enums: XmlEnum, XmlEnumValue

本文详细介绍了如何使用@XmlEnum注解定义XML中的枚举类型,并提供了具体的Java代码示例,展示了如何通过@XmlEnumValue注解指定枚举常量在XML中的表示形式。

An enum type is annotated with XmlEnum. It has an optional element value of type java.lang.Class which defines the class used for the values used in the XML representation. Usually, and by default, this is java.lang.String but other types, even numeric ones, are equally possible. For a straightforward enum type, this is sufficient:

@XmlEnum
public enum SubElemType {
   //...(enum definition)
}

Individual enum constants have to be annotated if there is a difference between the Java name and the string used to represent the value in XML. This is defined with an @XmlEnumValue annotation that is attached to individual enum constants. Its required element defines the XML representation string. If it might be useful for the Java application to have support for the conversion between Java values and XML representations as well, the enum type might define the XML representation as a parameter for the constructor, provide a getter for the XML string and perhaps even a lookup function (fromValue) to convert a string to the enum constant. Such a deluxe version of an enum type is shown below.

@XmlEnum
public enum SubElemType {
    @XmlEnumValue("PrMaSig")
    PR_MA_SIG("PrMaSig"),
    @XmlEnumValue("Track1")
    TRACK_1("Track1"),
    // ...(more enum constant definitions)

    private final String value;

    SubElemType(String v) {
        value = v;
    }

    public String value() {
        return value;
    }

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

 

转载于:https://www.cnblogs.com/huey/p/5512295.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值