users@jaxb.java.net

Re: JAXB one-to-many relationship problems.

From: Han Ming Ong <hanming_at_mac.com>
Date: Tue, 08 Apr 2003 10:17:01 -0700

On Tuesday, April 8, 2003, at 08:25 AM, SUBSCRIBE JAXB-INTEREST
CBroussard wrote:

> Hi All,
>
> I'm relatively new to jaxb and am experiencing problems.
>
> I'm trying to bind a one to many relationship and it doesn't seem to
> work
> for me.
>
> a sample xml file would be
>
> <root>
> <book>
> <title />
> <authors>
> <author>
> <firstname />
> <lastname />
> </author>
> <author>
> <firstname />
> <lastname />
> </author>
> </authors>
> </book>
> </root>
>
> i'm trying to bind through xml schemas instead of dtd.
>
> here's a sniplet.
>
> <xsd:complexType name="AuthorsType">
> <xsd:sequence>
> <xsd:element name="author" type="author"
> maxOccurs="unbounded"
> />
> </xsd:sequence>
> </xsd:complexType>
>
> <xsd:complexType name="author">
> <xsd:sequence>
> <xsd:element name="firstname" type="xsd:string" />
> <xsd:element name="lastname" type="xsd:string" />
>
> </xsd:sequence>
> </xsd:complexType>
>
> when i run it through xjc, it generates the interface of
>
> public interface AuthorsType {
> java.util.List getAuthor();
> }
>
> i want to see something like this???
>
> public interface AuthorsType {
> java.util.List getAuthor();
> void setAuthor(int index, Author author);
> }
>
> interface Author generates the correct interface.
>
> What am I doing wrong? I have tried many different configurations for
> the
> xsd file. However, this is the closest I've gotten thus far.
>
> Thanks,
>
> Chris

Setting the following right after the <schema> element as customization
will get you the 2nd method but now the 1st returns you a Java array.

   <xs:annotation>
     <xs:appinfo>
       <jxb:globalBindings collectionType="indexed"/>

       <jxb:schemaBindings>
         <jxb:package name="com.my.package.name"/>
       </jxb:schemaBindings>
     </xs:appinfo>
   </xs:annotation>

Han Ming

ps: there are more customizations that you can do, after reading the
documentation.