users@jaxb.java.net

Re: unmarshalling nested complex type elements

From: Wolfgang Laun <wolfgang.laun_at_gmail.com>
Date: Sun, 29 Mar 2009 18:10:59 +0200

Well, here's an XML schema corresponding to your sample XML:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:complexType name="SampleType">
    <xs:sequence>
      <xs:element name="INF" type="InfType" maxOccurs="unbounded"/>
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="InfType" mixed="true">
    <xs:sequence>
      <xs:element name="INF" type="InfType" maxOccurs="unbounded"/>
    </xs:sequence>
    <xs:attribute name="name" type="xs:string"/>
  </xs:complexType>
  <xs:element name="SAMPLE" type="SampleType"/>
</xs:schema>

Although, by looking into the generated InfType.java, you'll see that by
using
JAXB you won't gain much, compared to a plain DOM tree. You have a
field String name, but the list field content is just a List<Serializable>.
You'll have to apply instanceof to each list element to see whether
it is another enclosed <INF>, or the content of a leaf node.

-W

On Sat, Mar 28, 2009 at 11:59 AM, Eugene Ong <pebrian27_at_yahoo.com> wrote:

> Hi,
>
> I'm having a problem creating the proper classes and annotations for an XML
> with complex nested elements. Here is a sample XML:
>
> <SAMPLE>
> <INF name="DETAILS">
> <INF name="PHONE">5754837</INF>
> <INF name="GENDER">male</INF>
> </INF>
> <INF name="CONTENT">WOW</INF>
> </SAMPLE>
>
> The INF element can be a simple value or nested infinity!
>
> Changing the XML schema is not an options since this is already widely used
> in our system.
>
> Can this is be done using JAXB? Btw, this is my first time in using JAXB!
>
> Thanks very much,
> Eugene
>
> ------------------------------
> New Email addresses available on Yahoo!
> <http://sg.rd.yahoo.com/aa/mail/domainchoice/mail/signature/*http://mail.promotions.yahoo.com/newdomains/aa/>
> Get the Email name you've always wanted on the new @ymail and @rocketmail.
> Hurry before someone else does!
>