users@jaxb.java.net

inner element/class issue

From: Douglas Jackson <douglasscottjackson_at_yahoo.com>
Date: Fri, 07 Mar 2003 07:49:49 -0800

Hi!

I saw a post addressing part of this issue (the
fact that contained elements cannot be marshalled).

Given the schema below I expect getInnerObjects()
to produce a List of InnerClasses. It does not. It
produces a list of InnerTypeImpls. Is there a way
to get the a List of InnerClasses back?

-Doug.

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"

xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
           jaxb:version="1.0"
           elementFormDefault="qualified">

   <xs:annotation>
       <xs:appinfo>
          <jaxb:globalBindings
generateIsSetMethod="true"/>
          <jaxb:schemaBindings>
             <jaxb:package name="test"/>
          </jaxb:schemaBindings>
       </xs:appinfo>
   </xs:annotation>

   <xs:element name="outer">
      <xs:complexType>
         <xs:sequence>
            <xs:element name="inner" type="inner-type"
minOccurs="1" maxOccurs="unbounded"/>
         </xs:sequence>
         <xs:attribute name="name" type="xs:string"
use="required"/>
      </xs:complexType>
   </xs:element>

   <xs:element name="inner" type="inner-type"
minOccurs="1" maxOccurs="unbounded"/>

   <xs:complexType name="inner-type">
      <xs:annotation>
         <xs:appinfo>
            <jaxb:class name="InnerClass"/>
            <jaxb:property name="InnerObjects"/>
         </xs:appinfo>
      </xs:annotation>
      <xs:choice>
         <xs:element name="inner-one"
type="xs:string"/>
         <xs:element name="inner-two"
type="xs:string"/>
      </xs:choice>
   </xs:complexType>

</xs:schema>

===========================
Exception in thread "main"
java.lang.ClassCastException: test.impl.InnerTypeImpl
        at test.JunkTest.main(JunkTest.java:31)
------------
package test;

import javax.xml.bind.*;

public class JunkTest
{
   public static void main(String[] args)
      throws Exception
   {
      // create a JAXBContext capable of handling
classes generated into
      // the primer.po package
      JAXBContext jc = JAXBContext.newInstance( "test"
);

      // create an Unmarshaller
      Unmarshaller u = jc.createUnmarshaller();

      // unmarshal a po instance document into a tree
of Java content
      // objects composed of classes from the
primer.po package.
      Outer outer =
         (Outer)u.unmarshal(new
java.io.FileInputStream("d:/test/junk.xml"));

      java.io.ByteArrayOutputStream baos = new
java.io.ByteArrayOutputStream();
      jc.createMarshaller().marshal(outer, baos);
      System.out.println("outer - " +
baos.toString());

      InnerClass innerClass = (InnerClass)
outer.getInnerObjects().get(0);

      baos.reset();
      jc.createMarshaller().marshal(innerClass, baos);
      System.out.println("innerClass - " +
baos.toString());
   }
}


__________________________________________________
Do you Yahoo!?
Yahoo! Tax Center - forms, calculators, tips, more
http://taxes.yahoo.com/