两个@XmlJavaTypeAdapters用于JAXB中的一个@XmlAttribute?

时间:2011-06-06 14:46:03

标签: java jaxb

我有一个这样的课程:

@XmlRootElement(name = "PricingGroup")
public class PricingGroup {

    ...

    @XmlAttribute(name = "partyName")
    @XmlJavaTypeAdapter(CustomerGroupRelationships.Adapter.class)
    private List<BilltoCustomer> billtoCustomers = new ArrayList<BilltoCustomer>();

    @XmlAttribute(name = "partyName")
    @XmlJavaTypeAdapter(PartyNames.Adapter.class)
    private PartyName partyName;  

    ...  
}

似乎JAXB无法为一个属性(此处为@XmlJavaTypeAdapter)映射两个partyName。如果我注释掉billtoCustomers上的注释或partyName上的注释,则会从XML中读取另一个成员变量而不会出现问题。

如何同时获取这两个值?

1 个答案:

答案 0 :(得分:2)

您可以映射其中一个属性(partyName),然后使用afterUnmarshal事件派生其他属性(billToCustomers):

@XmlRootElement(name = "PricingGroup")
public class PricingGroup {

    ...

    @XmlTransient    
    private List<BilltoCustomer> billtoCustomers = new ArrayList<BilltoCustomer>();

    @XmlAttribute(name = "partyName")
    @XmlJavaTypeAdapter(PartyNames.Adapter.class)
    private PartyName partyName;  


   void afterUnmarshal(Unmarshaller u, Object parent) {
      // Derive billToCustomers from partyName
   }

    ...  
}