2017-11-25 108 views
0

使用Java进行开发时,我想将枚举编组为一个特定的XML结构。 比方说,我有一个非常简单枚举是这样的:Jaxb使用特定的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

这里需要使用自定义的适配器。请参阅[this](https://stackoverflow.com/questions/4656992/providing-custom-value-serialization-for-enums-via-jaxb)问题。 – chinoy

+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()); 

    } 

}