users@jaxb.java.net

Re: Unmarshal exception..

From: Bill Marcum <bmarcum_at_EVOKE.COM>
Date: Fri, 08 Nov 2002 16:19:33 -0700

Actually, I'm seeing a couple problems.
Problem one seems to revolve around my use of inheritance:
I define a type (interface), which I want my other objects to inherit from, like this:

    <xsd:complexType name="ICommand">
      <xsd:attribute name="transactionID" type="xsd:string"/>
      <xsd:attribute name="target" type="xsd:string"/>
    </xsd:complexType>

If I define an element which is of that type, with no additional attributes or elements, like this:

    <xsd:element name="LeaveCommand" type="ICommand"/>

It marshals, validates, and unmarshals successfully.

However, if I need to extend the interface to add additional elements or attributes, like this:

    <xsd:element name="CreateConferenceCommand">
    <xsd:complexType>
      <xsd:complexContent>
       <xsd:extension base="ICommand">
         <xsd:attribute name="ConferenceName" type="xsd:string"/>
       </xsd:extension>
      </xsd:complexContent>
    </xsd:complexType>
    </xsd:element>

I get a validation exception: "unexpected attribute "target""
But, it marshalls and unmarshals successfully.
I can work around this problem by defining ICommand with no attributes, and defining the target and
transactionID attributes in each element that extends it. (Or by not validating).



Problem two revolves around my use of interfaces to define a list of objects of different types.
This one is worse because I haven't been able to figure out a workaround:

    // ---------------------------------------------------
    // First, I defined a marker interface
    <xsd:complexType name="IAttribute"></xsd:complexType>

    // ---------------------------------------------------
    // Then, I defined a couple specific types which implement this interface
    <xsd:element name="EntityType">
    <xsd:complexType>
      <xsd:complexContent>
       <xsd:extension base="IAttribute">
         <xsd:attribute name="type" type="xsd:string"/>
       </xsd:extension>
      </xsd:complexContent>
    </xsd:complexType>
    </xsd:element>

    <xsd:element name="PresState">
    <xsd:complexType>
      <xsd:complexContent>
       <xsd:extension base="IAttribute">
         <xsd:attribute name="state" type="xsd:string"/>
       </xsd:extension>
      </xsd:complexContent>
    </xsd:complexType>
    </xsd:element>


    // ---------------------------------------------------
    // Then, I defined a class which contains a list of these attributes

    <xsd:complexType name="Presence">
      <xsd:sequence>
          <xsd:element name="Attributes" minOccurs="0" maxOccurs="unbounded" type="IAttribute"/>
          <xsd:element name="EntityID" type="EntityID"/>
      </xsd:sequence>
      <xsd:attribute name="endpointID" type="xsd:string"/>
    </xsd:complexType>
    <xsd:element name="PresenceElement" type="Presence"/>

If I construct a PresenceElement, and populate it with PresState and/or EntityType elements,
I get the following validation error:
ValidationException: tag name "com.raindance.appltools.jmessage.PresState" is not allowed.
Possible tag names are: <com.raindance.appltools.jmessage.IAttribute>
I can still marshal it, and get the following xml:

    <PresenceElement endpointID="me">
        <Attributes>
            <PresState state="ONLINE"></PresState>
        </Attributes>
        <EntityID ID="hey"></EntityID>
    </PresenceElement>

But, if I attempt to unmarshal this xml, I get: UnmarshalException: Unexpected element {}:PresState