users@jaxb.java.net

Re: Unmarshalling List/Array as root element

From: Wolfgang Laun <wolfgang.laun_at_gmail.com>
Date: Wed, 22 Sep 2010 20:02:24 +0200

There's no simpler way than writing this class with a single List<
PhonePreorder>. It's XML that defines that a document is a single element.
There are some ways for simplifying wrapped lists within a document, but you
need a root for the list even so.
-W

On 22 September 2010 19:14, Jarnis Bertelsen - TELMORE <jab_at_telmore.dk>wrote:

> 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.
>
>
>