开发者

How can I specify which instances of a class are unmarshalled using an XMLAdapter?

I have the following java class and have placed an XmlJavaAdapter annotation on the payerPartyReference variable. I want the adapter PartyReferenceAdapter to be used for unmarshalling ONLY this variable, not any other variables which have the same type of PartyReference, whether in this class 开发者_C百科or some other class. How can I do this? Thanks for your help!

public class InitialPayment extends PaymentBase
{

    // Want PartyReferenceAdapter to be used here
    @XmlJavaTypeAdapter(PartyReferenceAdapter.class)
    protected PartyReference payerPartyReference;
    //
    // Dont want PartyReferenceAdapter to be used here
    protected PartyReference receiverPartyReference;
    //
    protected AccountReference receiverAccountReference;
    @XmlSchemaType(name = "date")
    protected XMLGregorianCalendar adjustablePaymentDate;
    @XmlSchemaType(name = "date")
    protected XMLGregorianCalendar adjustedPaymentDate;
    protected Money paymentAmount;
}

My Adapter is defined as follows:

public class PartyReferenceAdapter 
    extends XmlAdapter < Object, PartyReference > {

  public PartyReference unmarshal(Object obj) throws Exception {

     Element element = null;

     if (obj instanceof Element) {

        element = (Element)obj;

        String reference_id = element.getAttribute("href");
        PartyReference pr = new PartyReference();
        pr.setHref(reference_id);                   
        return pr;

    }

    public Object marshal(PartyReference arg0) throws Exception {
     return null;
    }
}


Field/Property Level

If you set @XmlJavaTypeAdapter on a field/property it will only be used for that property.

  • http://bdoughan.blogspot.com/2010/07/xmladapter-jaxbs-secret-weapon.html

Type Level

If you set @XmlJavaTypeAdapter on a type, then it will used for all references to that type.

  • http://bdoughan.blogspot.com/2010/12/jaxb-and-immutable-objects.html

Package Level

If you set @XmlJavaTypeAdapter on a package, then it will be used for all references to that type within that package:

  • http://bdoughan.blogspot.com/2011/05/jaxb-and-joda-time-dates-and-times.html
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜