users@jaxb.java.net

Re: Lists and JAXB

From: Kohsuke Kawaguchi <Kohsuke.Kawaguchi_at_Sun.COM>
Date: Tue, 10 Oct 2006 11:03:59 -0700

Dan Diephouse wrote:
> Is it possible to marshal an ArrayList? i.e. I have List<Customer> and I
> want to do:
>
> element = new JAXBElement(customers, ArrayList<Customer>.class, qname)
> marshaller.marshal(element, writer);
>
> I don't think it is, but I'm wondering what the best way to serialize an
> ArrayList is if thats what I get handed...

This is one of the area that JAXB really needs to do better. It's just
that it's tricky because list can be list of anything.

Right now, the closest approximation is to turn List into array. You
need to create JAXBContext from the array class like:

   JAXBContext.newInstance(Customer[].class);

but then you can do:

  element = new JAXBElement(customers, Customer[].class, qname);

-- 
Kohsuke Kawaguchi
Sun Microsystems                   kohsuke.kawaguchi_at_sun.com