users@jaxb.java.net

Re: JAXB 2.0 and the JAXBElement

From: Kohsuke Kawaguchi <Kohsuke.Kawaguchi_at_Sun.COM>
Date: Wed, 02 Feb 2005 11:48:34 -0800

Brian Pontarelli wrote:
> Does anyone know why sometimes repeating elements are compiled into
> List<Foo> and other times into <List<JAXBElement<Foo>>? The XSD snippet
> I have that does the former is:

Some of those behaviors are still discussed in the EG, so it might still
change.

>
> <xs:element name="dataRequestBody">
> <xs:complexType mixed="false">
> <xs:sequence>
> <xs:element ref="dataRequest" maxOccurs="unbounded"/>
> </xs:sequence>
> </xs:complexType>
> </xs:element>
> <xs:element name="dataRequest">
> <xs:complexType mixed="false">
> <xs:attribute name="name" type="xs:string" use="required"/>
> </xs:complexType>
> </xs:element>

This should bind to List<DataRequest> because you've only got one kind
of element here.


> The XSD snippet that does the latter is:
>
> <xs:element name="data">
> <xs:complexType mixed="false">
> <xs:complexContent>
> <xs:extension base="baseParam">
> <xs:attribute name="scope" use="required">
> <xs:simpleType>
> <xs:restriction base="xs:NCName">
> <xs:enumeration value="application"/>
> <xs:enumeration value="view"/>
> </xs:restriction>
> </xs:simpleType>
> </xs:attribute>
> <xs:attribute name="type" use="required">
> <xs:simpleType>
> <xs:restriction base="xs:NCName">
> <xs:enumeration value="byte"/>
> <xs:enumeration value="short"/>
> <xs:enumeration value="char"/>
> <xs:enumeration value="int"/>
> <xs:enumeration value="long"/>
> <xs:enumeration value="float"/>
> <xs:enumeration value="double"/>
> <xs:enumeration value="String"/>
> </xs:restriction>
> </xs:simpleType>
> </xs:attribute>
> <xs:attribute name="arrayDepth" type="xs:integer"
> use="required"/>
> </xs:extension>
> </xs:complexContent>
> </xs:complexType>
> </xs:element>
> <xs:element name="dataBody">
> <xs:complexType mixed="false">
> <xs:sequence>
> <xs:element ref="data" minOccurs="0" maxOccurs="unbounded"/>
> </xs:sequence>
> </xs:complexType>
> </xs:element>

I'd think that this should also bind to List<Data>. But sounds like you
are saying that it doesn't.

Could you send me the whole schema so that I can investigate?


> Does it have anything to do with the extension? Because besides the
> extension and the simpleType for attributes in the second example, they
> are essentially the same XSD snippet.

Currently, we use JAXBElement when:

        1) element substitutions can happen
        2) more than one element can occur in the property,
           and when they cannot be distinguished by types
        3) for arcane reasons we need to use JAXBElement so as not
           to confuse the runtime.


-- 
Kohsuke Kawaguchi
Sun Microsystems                   kohsuke.kawaguchi_at_sun.com