users@jaxb.java.net

Re: XML element polymorphism??

From: Kohsuke Kawaguchi <Kohsuke.Kawaguchi_at_Sun.COM>
Date: Mon, 02 Jun 2003 08:30:55 -0700

Filip Rindler <filip.rindler_at_gmx.de> wrote:
> This XML encoder mentioned above always polymorphically returns a XMLBMatrix
> element (that's the XMLB class corresponding to the Matrix element type),
> i.e. the runtime type is a subclass of XMLBMatrix. This works, because JAXB
> is as smart as to recognize the <xsd:extension base="Matrix"> tag and let
> XMLBMatrix2DGrid (XMLB class corresponding to the Matrix2DGrid element type)
> extend the XMLBMatrix class. In the schema above it might become clear, that
> the 'Matrix' element type could be called 'abstract', because it will never
> actually occur in the document, only its derived elements (such as
> Matrix2DGrid) will occur.

I'm bit confused since there's just that "Matrix" complex type, no
"Matrix" element. And I have no idea where XMLB comes from. Is it the
prefix you assigned? Plus what do you mean by "element type"? Is it
"element decl" or "complex type"?


> OK, now after this long tale, here's my problem: While it seems to work to
> just assign a XMLBMatrix2DGrid object to the 'mtxfile' root element using
> its generated setMatrix() method, i get a validation error. So is there a

That's probably because you are trying to use the type substitution here
and it's one of the unsupported features in 1.0.


> I'm not stubborn on the stuff I've written so far and if there's another way
> of accomplishing my goal, I can change everything, just to have that said
> ;-)

I think one possibility is to define your schema as:

<xsd:complexType name="MTXFile">
 <xsd:sequence>
  <xsd:element name="minVersion" type="xsd:int" />
  <xsd:choice>
    <xs:annotation><xsd:appinfo>
      <jaxb:property name="Matrix" />
    </xsd:appinfo></xsd:annotation>
    <xsd:element name="matrix2DGrid" type="Matrix2DGrid" />
    <xsd:element name="matrix3DGrid" type="Matrix3DGrid" />
    ...
  </xsd:choice>
 </xsd:sequence>
</xsd:complexType>

The property customization will map the choice into a single property,
so you can just say:

    mtxFile.setMatrix( new Matrix3DGrid() );

or

    mtxFile.setMatrix( new Matrix2DGrid() );




regards,
--
Kohsuke KAWAGUCHI                  408-276-7063 (x17063)
Sun Microsystems                   kohsuke.kawaguchi_at_sun.com