users@jaxb.java.net

Re: InstantiationException when using restrictions and abstract schema type...

From: Iaroslav Savytskyi <iaroslav.savytskyi_at_oracle.com>
Date: Wed, 16 Jan 2013 17:32:08 +0100

Hi, Luke,

I think you have to change your XML a bit. While unmarshalling JAXB
doesn't know which class to instantiate as Header's BaseHeaderType. So
changing xml to:

<?xml version="1.0" encoding="UTF-8"?>
<Structure>
    <Header xsi:type="StructureHeaderType"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
       <ID>Sample_DSD1_With_No_Group</ID>
    </Header>
</Structure>

should fix the problem.

--
Best regards.
Yaroslav
On 16/01/13 11:39, tontonflingueur005-javadotnet_at_yahoo.fr wrote:
> Hi,
>
> I am trying to use jaxb 2.2.6-ri to marshall/unmarshall xml documents
> using the SDMX (Statistical Data Metadata Exchange), and I am
> encountering
> an InstantiationException related to use of abstract classes and
> derivation by restriction.
>
> The complete schema is rather complex
> (you can download it at the following location
> http://sdmx.org/wp-content/uploads/2012/11/SDMX_2-1_SECTION_3A_SDMX_ML.
> zip),
> but I was able to track down the problem I am encountering tp the
> following small
> sample.
>
> Here is the schema (sdmx_reduced.xsd) :
>
> <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
> <xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
>
>    <xs:element name="Structure" type="StructureType"/>
>
>    <xs:complexType name="StructureHeaderType">
>      <xs:complexContent>
>        <xs:restriction base="BaseHeaderType">
> 	<xs:sequence>
> 	<xs:element name="ID" type="xs:string"/>
> 	</xs:sequence>
>        </xs:restriction>
>      </xs:complexContent>
>    </xs:complexType>
>
>    <xs:complexType name="StructureType">
>      <xs:complexContent>
>        <xs:restriction base="MessageType">
> 	<xs:sequence>
> 	<xs:element name="Header" type="StructureHeaderType"/>
> 	</xs:sequence>
>        </xs:restriction>
>      </xs:complexContent>
>    </xs:complexType>
>
>    <xs:complexType name="MessageType" abstract="true">
>      <xs:sequence>
>        <xs:element name="Header" type="BaseHeaderType"/>
>      </xs:sequence>
>    </xs:complexType>
>
>    <xs:complexType name="BaseHeaderType" abstract="true">
>      <xs:sequence>
>        <xs:element name="ID" type="xs:string"/>
>      </xs:sequence>
>    </xs:complexType>
>
> </xs:schema>
>
> In this schema, I have used the same  names as in the original
> SDMX schema, but have ommited namespaces. I generated Java classes
> using :
>
> 	xjc.sh sdmx_reduced.xsd
>
> I am using the following code java code to unmarshall the xml document
>
> 	 JAXBContext jAXBContext =
> JAXBContext.newInstance("generated");
> 	 Unmarshaller unmarshaller = jAXBContext.createUnmarshaller();
> 	 StructureType structure=
> (StructureType)unmarshaller.unmarshal(
> 	     new File("/tmp/structure.xml"));
>
> This code triggers an InstantiationException.
> Here is the content of the /tmp/structure.xml file :
>
> <?xml version="1.0" encoding="UTF-8"?>
> <Structure>
>     <Header>
>        <ID>Sample_DSD1_With_No_Group</ID>
>     </Header>
> </Structure>
>
> My interpretation of the problem is that the StructureType class
> generated by xjc does not contain any reference whatsoever to the
> StructureHeaderType class, so that, when encountering the Header
> element, the
> marshaller has no other choice than using the abstract HeaderType
> class.
>
> Do you guys have any idea of a workaround that would not involve
> modifying the generated code ?
>
> Cheers,
> Luke