Sylvain,

I'll be looking into in detail this somewhen today (as time permits). There's one things that I noticed evebn when only reading briefly through your email.

You start with an XML schema, and use Castor's code generator to create Java classes that match the XML schema definitions. In addition to the domain classes generated, there will be a second set of classes generated, which we here call 'descriptor classes'. This second set of classes holds all meta information needed for un-/marshalling that cannot be stored in the domain classes, such as namespaces, element names, etc.

As these classes have been generated already, please do *not* throw them away. In other words, keep them, as they will be required during un-/marshalling, otherwise you'll break the contract as defined in your XML schema.

When you keep these classes around, you do *not* have to write a mapping file, either.

Werner

Sylvain Lemasson wrote:

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 <http://www.trueswitch.com/yahoo-fr/> vers Yahoo! Mail


---------------------------------------------------------------------
To unsubscribe from this list please visit:

   http://xircles.codehaus.org/manage_email

Reply via email to