Jaxb使用特定的XML结构编组枚举

时间:2017-11-25 16:21:29

标签: java xml enums jaxb

使用Java开发,我想将枚举编组为特定的XML结构。 假设我有一个非常简单的枚举:

public enum CustomEnum {
    CUSTOM_ENUM_VALUE_1 ("value 1"),
    CUSTOM_ENUM_VALUE_2 ("value 2"),
    CUSTOM_ENUM_VALUE_3 ("value 3");

    private String value;

    CustomEnum(String value) {
        this.value = value;
    }
}

我想marhsal这样的结构:

<custom-enum>
    <key>CUSTOM_ENUM_VALUE_1</key>
    <value>value 1</value>
</custom-enum>

我尝试过这样的注释:

@XmlRootElement (name="custom-enum")
public enum CustomEnum {
    CUSTOM_ENUM_VALUE_1 ("value 1"),
    CUSTOM_ENUM_VALUE_2 ("value 2"),
    CUSTOM_ENUM_VALUE_3 ("value 3");

    private String value;

    CustomEnum(String value) {
        this.value = value;
    }

    @XmlElement (name="value")
    public String getValue() {
        return value;
    }

    @XmlElement (name="key")
    public String getName() {
        return name();
    }
}

但它不起作用......

1 个答案:

答案 0 :(得分:0)

您可以在此处使用XMLAdapter,如下所示

枚举

package com.test.jaxb;

public enum CustomEnum {
    CUSTOM_ENUM_VALUE_1 ("value 1"),
    CUSTOM_ENUM_VALUE_2 ("value 2"),
    CUSTOM_ENUM_VALUE_3 ("value 3");

    private String value;

    CustomEnum(String value) {
        this.value = value;
    }

    public String getValue() {
        return this.value;
    }

    public CustomEnum fromValue(String value) {
        CustomEnum result = null;
        for (CustomEnum enumVal : CustomEnum.values()) {
            if (enumVal.value.equals(value)) {
                result = enumVal;
                break;
            }
        }
        if(result == null) {
            throw new IllegalArgumentException("CustomEnum does not supported for value " + value);
        }
        return result;
    }
}

POJO相当于编组形式

package com.test.jaxb;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;

@XmlAccessorType(XmlAccessType.FIELD)
public class AdaptedEnum {
    @XmlElement(name="key")
    private String enumName;
    @XmlElement(name="value")
    private String enumvalue;

    public String getEnumName() {
        return enumName;
    }

    public void setEnumName(String enumName) {
        this.enumName = enumName;
    }

    public String getEnumvalue() {
        return enumvalue;
    }

    public void setEnumvalue(String enumvalue) {
        this.enumvalue = enumvalue;
    }

}

适配器

package com.test.jaxb;

import javax.xml.bind.annotation.adapters.XmlAdapter;

public class Adapter extends XmlAdapter<AdaptedEnum, CustomEnum> {

    @Override
    public AdaptedEnum marshal(CustomEnum arg0) throws Exception {
        AdaptedEnum result = new AdaptedEnum();
        result.setEnumName(arg0.name());
        result.setEnumvalue(arg0.getValue());
        return result;
    }

    @Override
    public CustomEnum unmarshal(AdaptedEnum arg0) throws Exception {
        return CustomEnum.valueOf(arg0.getEnumvalue());
    }


}

消费者根元素

package com.test.jaxb;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

@XmlRootElement(name="root")
public  class  RootElement {

    private CustomEnum customEnum = CustomEnum.CUSTOM_ENUM_VALUE_1;

    @XmlJavaTypeAdapter(Adapter.class)
    @XmlElement(name="cusom-enum")
    public synchronized CustomEnum getCustomEnum() {
        return customEnum;
    }

    public synchronized void setCustomEnum(CustomEnum customEnum) {
        this.customEnum = customEnum;
    }


}

测试代码

package com.test.jaxb;

import java.io.StringReader;

import javax.xml.bind.JAXB;
import javax.xml.bind.JAXBException;

public class JAXBTester {

    public static void main(String[] args) throws JAXBException {
        JAXB.marshal(new RootElement(), System.out);
        String input = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\r\n" + 
                "<root>\r\n" + 
                "    <cusom-enum>\r\n" + 
                "        <key>CUSTOM_ENUM_VALUE_1</key>\r\n" + 
                "        <value>value 1</value>\r\n" + 
                "    </cusom-enum>\r\n" + 
                "</root>";
        RootElement root = JAXB.unmarshal(new StringReader(input), RootElement.class);
        System.out.println(root.getCustomEnum());

    }

}
相关问题