Hello, I need to read a document which contents derived types with the "xsi:type" attribute.
I've tried to implement the example given in the XML Schema Recommandation. see : 4.3 Using Derived Types in Instance Documents http://www.w3.org/TR/xmlschema-0/#UseDerivInInstDocs (see my sources below) I generate the source code from the xsd with the SourceGenerator. When I unmarshal the document I get the following error : unable to add 'shipTo' to <purchaseOrder> due to the following exception: >>>--- Begin Exception ---<<< java.lang.IllegalStateException: java.lang.ClassCastException at ipo.binding.PurchaseOrderTypeDescriptor$2.setValue PurchaseOrderTypeDescriptor.java:121) at org.exolab.castor.xml.UnmarshalHandler.endElement(UnmarshalHandler.java:1066) ... But if I make the following to the source code it works : Class PurchaseOrderType ........ private ipo.binding.ShipTo _shipTo; >change> private ipo.binding.Address _shipTo; ........ public void setShipTo(ipo.binding.ShipTo shipTo) >change> public void setShipTo(ipo.binding.Address shipTo) ........ public ipo.binding.ShipTogetShipTo() >change> public ipo.binding.Address getShipTo() Class PurchaseOrderTypeDescriptor ........ target.setShipTo( (ipo.binding.ShipTo) value); >change> target.setShipTo( (ipo.binding.Address) value); (and idem for BillTo) My question is : do you have a solution to unmarshall this kind of document without modifying the source code ? (or did I miss something ?) Thanks a lot for your help, Jean Witte -- ipo.xml -------------------------------- <?xml version="1.0"?> <ipo:purchaseOrder orderDate="1999-12-01" xmlns:ipo="http://www.example.com/IPO" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.com/IPO ipo.xsd"> <shipTo exportCode="1" xsi:type="ipo:UKAddress"> <name>Helen Zoe</name> <street>47 Eden Street</street> <city>Cambridge</city> <!--<postcode>CB1 1JR</postcode>--> </shipTo> <billTo xsi:type="ipo:USAddress"> <name>Robert Smith</name> <street>8 Oak Avenue</street> <city>Old Town</city> <state>AK</state> <zip>95819</zip> </billTo> <items> <item partNum="833-AA"> <productName>Lapis necklace</productName> <quantity>1</quantity> <USPrice>99.95</USPrice> <ipo:comment>Want this for the holidays</ipo:comment> <shipDate>1999-12-05</shipDate> </item> </items> </ipo:purchaseOrder> -- ipo.xsd -------------------------------- <schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:ipo="http://www.example.com/IPO" targetNamespace="http://www.example.com/IPO"> <annotation> <documentation xml:lang="en"> International Purchase order schema for Example.com Copyright 2000 Example.com. All rights reserved. </documentation> </annotation> <!-- include address constructs --> <include schemaLocation="address.xsd"/> <element name="purchaseOrder" type="ipo:PurchaseOrderType"/> <element name="comment" type="string"/> <complexType name="PurchaseOrderType"> <sequence> <element name="shipTo" type="ipo:Address"/> <element name="billTo" type="ipo:Address"/> <element ref="ipo:comment" minOccurs="0"/> <element name="items" type="ipo:ItemsType"/> </sequence> <attribute name="orderDate" type="date"/> </complexType> <complexType name="ItemsType"> <sequence> <element name="item" minOccurs="0" maxOccurs="unbounded"> <complexType> <sequence> <element name="productName" type="string"/> <element name="quantity"> <simpleType> <restriction base="positiveInteger"> <maxExclusive value="100"/> </restriction> </simpleType> </element> <element name="USPrice" type="decimal"/> <element ref="ipo:comment" minOccurs="0"/> <element name="shipDate" type="date" minOccurs="0"/> </sequence> <attribute name="partNum" type="ipo:SKU" use="required"/> </complexType> </element> </sequence> </complexType> <simpleType name="SKU"> <restriction base="string"> <pattern value="\d{3}-[A-Z]{2}"/> </restriction> </simpleType> </schema> -- address.xsd -------------------------------- <schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:ipo="http://www.example.com/IPO" targetNamespace="http://www.example.com/IPO"> <annotation> <documentation xml:lang="en"> Addresses for International Purchase order schema Copyright 2000 Example.com. All rights reserved. </documentation> </annotation> <complexType name="Address"> <sequence> <element name="name" type="string"/> <element name="street" type="string"/> <element name="city" type="string"/> </sequence> </complexType> <complexType name="USAddress"> <complexContent> <extension base="ipo:Address"> <sequence> <element name="state" type="ipo:USState"/> <element name="zip" type="positiveInteger"/> </sequence> </extension> </complexContent> </complexType> <complexType name="UKAddress"> <complexContent> <extension base="ipo:Address"> <sequence> <!--<element name="postcode" type="ipo:UKPostcode"/>--> </sequence> <attribute name="exportCode" type="positiveInteger" fixed="1"/> </extension> </complexContent> </complexType> <!-- other Address derivations for more countries --> <simpleType name="USState"> <restriction base="string"> <enumeration value="AK"/> <enumeration value="AL"/> <enumeration value="AR"/> <!-- and so on ... --> </restriction> </simpleType> <!-- simple type definition for UKPostcode --> </schema> -- Main.java -------------------------------- public static void main(String[] args) throws IOException, MarshalException, ValidationException { File inputFile = new File("./structures/w3c-rec/ipo.xml"); InputSource source = new InputSource(new FileInputStream(inputFile)); source.setEncoding("utf-8"); source.setSystemId(inputFile.getPath()); PurchaseOrder po = (PurchaseOrder) new Unmarshaller(PurchaseOrder.class).unmarshal(source); System.out.println("Street : " + po.getBillTo().getStreet()); System.out.println("Street : " + ((USAddress) po.getBillTo()).getState()); } __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com ------------------------------------------------- If you wish to unsubscribe from this list, please send an empty message to the following address: [EMAIL PROTECTED] -------------------------------------------------

