users@jaxb.java.net

Unmarshalling List/Array as root element

From: Jarnis Bertelsen - TELMORE <jab_at_telmore.dk>
Date: Wed, 22 Sep 2010 19:14:39 +0200

Hey Guys

 

I'm fairly new to JAXB and I'll apologize in advance if my question is
very trivial, or my terminology is a bit off. Here goes:

I'm unmarshalling a list of custom Java objects from a web service. The
XML I receive is in this format:

 

<phonePreorders>

                             <phonePreorder>

 
<emailAddress>foo_at_somewhere.com</emailAddress>

 
<phoneNumber>55556666</phoneNumber>

 
<preOrderCompleteDate>2010-21-22T06:09:29CEST</preOrderCompleteDate>

                             </phonePreorder>

                             <phonePreorder>

 
<emailAddress>bar_at_somewhere.com </emailAddress>

 
<phoneNumber>77778888</phoneNumber>

 
<preOrderCompleteDate>2010-21-22T06:09:29CEST</preOrderCompleteDate>

                             </phonePreorder>

</phonePreorders>

(there are actually a few more attributes to each phonePreorder, but
this has no relevance to the question)

 

Each phonePreorder is generated from a @XmlRootElement annotated Java
class. Unmarshalling a single phonePreorder using JAXB is not a problem.
I'm having problems with the list however. I would prefer not to
introduce a new Java class holding the list of phonePreorders, as there
is really no need that a simple array or an ArrayList (or other
Collection class for that matter) can't take care of.

 

The client code looks like this:

JAXBContext context = JAXBContext.newInstance(PhonePreorder[].class,
PhonePreorder.class);

Unmarshaller unmarshaller = context.createUnmarshaller();

Object temp = unmarshaller.unmarshal(connection.getInputStream());

result = (PhonePreorder[])
unmarshaller.unmarshal(connection.getInputStream());

 

which results in a JAXBException with this message: unexpected element
(uri:"", local:"phonePreorders"). Expected elements are
<{}phonePreorder>

 

Is there a way to tell JAXB to unmarshall phonePreorders to an existing
collection class, without creating new annotated class to explain this?

 

Thanks in advance

Jarnis Bertelsen

Developer

TELMORE A/S

 

PS. The webservice was developed inhouse using Enunciate. While it IS
possible for me to modify the webservice, I'd prefer not to, as it is
already in use.