Problem unmarshalling XML - file useing inheritance
843834May 14 2003 — edited May 15 2003My application uses an xml - schema that describes a scene containing geometric objects. I use simple objects (sphere, box...) and complex objects (groups). every object is derived from the base type "node_type". I create Java-Files from this schema and this works fine. but when I try to unmarshal an xml file written according to the schema I receive an exception. It seems like the unmarshaller does not notice the "xsi:type" attribute. Everything works fine if I do not use inheritance.
Is there anything I do not see? Do I have to consider anything special if i want to use derived types in my xml file?
Thanks a lot,
Katja
// ------------ part of the xml schema i use -------- //
<xs:complexType name="group_type">
<xs:complexContent>
<xs:extension base="node_type">
<xs:sequence>
<xs:element name="child" type="node_type" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="sphere_type">
<xs:complexContent>
<xs:extension base="node_type">
<xs:sequence>
<xs:element name="radius" type="xs:float"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
// ---------- the xml file i am trying to unmarshal -------- //
<?xml version="1.0" encoding="UTF-8"?>
<!-- edited by Katja Hofmann -->
<xml_scene xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="geometry.xsd">
<xml_group> <!-- xml_group is of type group_type -->
<child xsi:type="sphere_type">
<radius>0.3</radius>
</child>
</xml_branch>
</xml_scene>
// ------------ the exception I get ----------------------- //
!! -- class javax.xml.bind.UnmarshalException was thrown during operation!
Unexpected element {}:height
javax.xml.bind.UnmarshalException: Unexpected element {}:height
at com.sun.xml.bind.unmarshaller.UnreportedException.createUnmarshalException(UnreportedException.java:59)
at com.sun.xml.bind.unmarshaller.SAXUnmarshallerHandlerImpl.reportAndThrow(SAXUnmarshallerHandlerImpl.java:406)
at com.sun.xml.bind.unmarshaller.SAXUnmarshallerHandlerImpl.startElement(SAXUnmarshallerHandlerImpl.java:96)
at org.apache.crimson.parser.Parser2.maybeElement(Parser2.java:1488)
at org.apache.crimson.parser.Parser2.content(Parser2.java:1779)
at org.apache.crimson.parser.Parser2.maybeElement(Parser2.java:1507)
at org.apache.crimson.parser.Parser2.content(Parser2.java:1779)
at org.apache.crimson.parser.Parser2.maybeElement(Parser2.java:1507)
at org.apache.crimson.parser.Parser2.content(Parser2.java:1779)
at org.apache.crimson.parser.Parser2.maybeElement(Parser2.java:1507)
at org.apache.crimson.parser.Parser2.parseInternal(Parser2.java:500)
at org.apache.crimson.parser.Parser2.parse(Parser2.java:305)
at org.apache.crimson.parser.XMLReaderImpl.parse(XMLReaderImpl.java:442)
at com.sun.xml.bind.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:139)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:129)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:134)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:138)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:151)
... and so on ... :-(