我有这样的课:
@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读取另一个成员变量就不会出现问题。如何同时获得两个值?
最佳答案
您可以映射一个属性(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
}
...
}