users@jaxb.java.net

Re: Bind existing class to existing schema HOWTO

From: Dmitri Colebatch <colebatchd_at_gmail.com>
Date: Tue, 21 Jun 2005 17:40:13 +1000

I'm not sure I really responded to this part of it before.

> On 6/21/05, Sekhar Vajjhala <Sekhar.Vajjhala_at_sun.com> wrote:
> > For java->schema, JAXB 2.0 does really try to handle as many of the
> > existing classes as possible.
> > So non Javabeans can be handled using @XmlJavaTypeAdapter.
> > By tweak are you talking about tweaking bits and pieces, are you
> > referring to
> > tweaking application code or adding annotations to get the desired
> > XML/schema ?

I've now got the XmlJavaTypeAdapter annotation working for me and it
does exactly what I was after. For the archives' benefit, here's my
initial problem restated:

I have two classes

class Deal
{
  Customer customer;
}

class Customer
{
 String name;
 String licenseNumber;
}

etc. the problem I see is that I can see how to get:

<Deal>
 <Customer>
   <Name>Dmitri Colebatch</Name>
   <LicenseNumber>123456789</LicenseNumber>
 </Customer>
</Deal>

but what I want is

<Deal>
 <Customer>
   <IndividualCustomer>
     <Name>Dmitri Colebatch</Name>
     <LicenseNumber>123456789</LicenseNumber>
   </IndividualCustomer>
 </Customer>
</Deal>

So I already have customer mapping essentially the way I want, but for
the level of indirection. Using the XmlJavaTypeAdapter annotation I
was able to do this:

class Deal
{
  @XmlJavaTypeAdapter(MyCustomerXmlAdapter.class)
  Customer customer;
}

public class CustomerXmlAdapter extends XmlAdapter<XmlCustomer,Customer>
{
  public CustomerDelegate unmarshal(XmlCustomer xmlCustomer)
  {
    return xmlCustomer.getIndividualBuyer();
  }

  public XmlCustomer marshal(Customer customer)
  {
    XmlCustomer xmlCustomer = new XmlCustomer();
    xmlCustomer.setIndividualBuyer(customer);
    return xmlCustomer;
  }
}

public class XmlCustomer
{
  private CustomerDelegate customer;

  public CustomerDelegate getIndividualBuyer()
  {
    return customer;
  }

  public void setIndividualBuyer(CustomerDelegate customer)
  {
    this.customer = customer;
  }
}

Hopefully this will help someone (o: note that I've done some notepad
editing to make the above code simpler so its possible there's a typo
there somewhere.

cheers, and thanks a lot to Sekhar for the pointer
dim