I have make a test with only one schema and it works fine. The unmarshalling
and the marshalling does not rase any error.
So the issue arise when two schemas are used.
Sylvain
----- Message d'origine ----
De : Sylvain Lemasson <[EMAIL PROTECTED]>
À : [email protected]
Envoyé le : Dimanche, 16 Septembre 2007, 13h20mn 28s
Objet : [castor-user] Tr : Tr : unmarsalling xml with xsi:type
Hello,
I have the following xsd files:
parent.xsd
<?xml version="1.0" encoding="UTF-8"?>
<schema
xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/parent"
xmlns:tns="http://www.example.org/parent">
<complexType name="vehicle" abstract="true">
<sequence>
<element name="name" minOccurs="1" maxOccurs="1"></element>
</sequence>
</complexType>
<complexType name="travel">
<sequence>
<element name="myvehicle" type="tns:vehicle">
</element>
</sequence>
</complexType>
<element name="root">
<complexType>
<sequence>
<element name="travel" type="tns:travel" minOccurs="1"
maxOccurs="1"></element>
</sequence>
</complexType>
</element>
</schema>
child.xsd
<?xml version="1.0" encoding="UTF-8"?>
<schema
xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/child"
xmlns:tns="http://www.example.org/child"
xmlns:parent="http://www.example.org/parent">
<complexType name="car" abstract="false">
<complexContent>
<extension base="parent:vehicle">
<sequence>
<element name="color" type="string"></element>
</sequence>
</extension>
</complexContent>
</complexType>
</schema>
Using the attribute org.exolab.castor.builder.javaclassmapping=type the
generator create four classes: Root,Car,Vegicle,Travel.
The marshalling works fine, but I always got unmarshalling error using "unable
to find FieldDescriptor for 'myvehicle' in ClassDescriptor of travel" even when
using a mapping file.
I try first with Unmarshaller.unmarshal(Root.class, reader);
Then with
Mapping mapping = new Mapping();
mapping.loadMapping("mapping.xml");
Unmarshaller unmarshaller = new Unmarshaller(mapping);
where mapping.xml contains:
<?xml version="1.0"
encoding="UTF-8"?>
<mapping>
<class name="com.example.castor.type.Root">
<map-to xml="root"/>
<field name="travel"
type="com.example.castor.type.Travel"
direct="false">
<bind-xml name="travel" node="element"/>
</field>
</class>
<class name="com.example.castor.type.Travel">
<map-to xml="travel"/>
<field name="myvehicle"
type="com.example.castor.type.Vehicle"
direct="false">
<bind-xml node="element"
auto-naming="deriveByClass"/>
</field>
</class>
<class name="com.example.castor.type.Car">
<map-to xml="car"/>
</class>
<class name="com.example.castor.type.Vehicle">
<map-to xml="vehicle"/>
</class>
</mapping>
May be something is wrong in the mapping file, I have set it base on the
example in the documentation "XML Mapping, xsi-type".
public static void main(String[] args) {
try {
StringWriter writer = new StringWriter();
com.example.castor.type.Car car = new com.example.castor.type.Car();
car.setColor("blue");
car.setName("titine");
Travel travel = new Travel();
travel.setMyvehicle(car);
Root root = new Root();
root.setTravel(travel);
root.marshal(writer);
String xml = writer.getBuffer().toString();
System.out.println(xml);
StringReader reader = new StringReader(xml);
Mapping mapping = new Mapping();
mapping.loadMapping("mapping.xml");
Unmarshaller unmarshaller = new Unmarshaller(mapping);
//Unmarshaller.unmarshal(Root.class, reader);
root = (Root)unmarshaller.unmarshal(reader);
} catch (Exception e) {
e.printStackTrace();
}
}
Please advice.
Sylvain
Ne gardez plus qu'une seule adresse mail ! Copiez vos mails vers Yahoo! Mail
_____________________________________________________________________________
Ne gardez plus qu'une seule adresse mail ! Copiez vos mails vers Yahoo! Mail