带有子类型的Jaxb枚举

时间:2014-06-21 11:26:48

标签: java enums jaxb

我有Country Enum。

@XmlEnum
public enum Country {    
 ....,ES,FR,....;
}

国家/地区用于产品:

public class Product {
    ...
    @XmlElement(name = "Country")
    private List<Country> origin;
    ...
}

产生的输出是:

<Product>
    <Name>egestas</Name>
    <Description>montes</Description>
    <Country>ES</Country>
    <Country>FR</Country>
</Product>

问题是我需要产生这种输出。

<Product>
    <Name>egestas</Name>
    <Description>montes</Description>
    <Country>
        <ID>ES</ID>
    </Country>
    <Country>
        <ID>FR</ID>
    </Country>
</Product>

如何在不使用适配器的情况下使用Enums生成以后的输出?

1 个答案:

答案 0 :(得分:0)

A

class Country {
    private CountryId ID; //...
}

enum CountryId { ..., ES, FR,... }

应按要求生成XML。

<强>后来

当然,这需要更多代码来创建具有一个附加层的元素。

Product p = ...;
Country es = new Country();
es.setID( CountryId.ES );
p.getCountry().add( es );