Hi
    I have a poblem with castor unmarshalling method. Let's be pragmatic and explain what i did exactly :
 
    I have the following schemas :
 
ImporteR.XSD:
<?
xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="http://www.toto.com" xmlns:imported="http://imported.ns.com" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://www.toto.com">
<xs:import namespace="http://imported.ns.com" schemaLocation="imported.xsd"/>
<xs:element name="Personne">
<xs:complexType>
<xs:sequence>
<xs:element ref="tns:Nom"/>
<xs:element ref="tns:Prenom"/>
<xs:element name="Adresse" type="imported:AdresseType"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Nom" type="xs:string"/>
<xs:element name="Prenom" type="xs:string"/>
</xs:schema>
 
ImporteD.XSD:
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema targetNamespace="http://imported.ns.com" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:complexType name="AdresseType">
<xsd:all>
<xsd:element name="Numero" type="xsd:int"/>
<xsd:element name="TypeRue" type="xsd:string"/>
<xsd:element name="NomRue" type="xsd:string"/>
<xsd:element name="CodePostal" type="xsd:int"/>
<xsd:element name="Ville" type="xsd:string"/>
</xsd:all>
</xsd:complexType>
</xsd:schema>
 
These schema are processed successfully via Castor using the "type" class generation and the "generated imported schema types"  option.
 
Here is the code i used to generate Java implementations:
 
SourceGenerator sg = new SourceGenerator();
        try {
            Properties builderConfiguration = sg.getDefault();
            builderConfiguration.put("org.exolab.castor.builder.javaclassmapping", "type");
            sg.setGenerateImportedSchemas(true);
            sg.generateSource("importer.xsd", "input");
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
 
This results in correct Java classes generation in correct package.
 
I have generated a smaple XML file that respects these schema (I used the Personne element):
 
ImporteR.XML:
<?xml version="1.0" encoding="UTF-8"?>
<tns:Personne xmlns:tns="http://www.toto.com" xmlns:p="http://imported.ns.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.toto.com importer.xsd http://imported.ns.com imported.xsd ">
<tns:Nom>tns:Nom</tns:Nom>
<tns:Prenom>tns:Prenom</tns:Prenom>
<Adresse>
<Numero>0</Numero>
<TypeRue>TypeRue</TypeRue>
<NomRue>NomRue</NomRue>
<CodePostal>0</CodePostal>
<Ville>Ville</Ville>
</Adresse>
</tns:Personne>
 
Finally, I use the followinf code to unmarshall this XML into Personne classes:
        try {
            Personne personne = (Personne) Personne.unmarshalPersonne(new FileReader("importer.xml"));
        } catch (MarshalException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ValidationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

And i get this on the output console :
org.xml.sax.SAXException: unable to find FieldDescriptor for 'Nom' in ClassDescriptor of Personne
at org.exolab.castor.xml.UnmarshalHandler.startElement(UnmarshalHandler.java:1791)
at org.exolab.castor.xml.UnmarshalHandler.startElement(UnmarshalHandler.java:1292)
at org.apache.xerces.parsers.AbstractSAXParser.startElement(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanStartElement(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at org.exolab.castor.xml.Unmarshaller.unmarshal(Unmarshaller.java:605)
at org.exolab.castor.xml.Unmarshaller.unmarshal(Unmarshaller.java:513)
at org.exolab.castor.xml.Unmarshaller.unmarshal(Unmarshaller.java:691)
at input.Personne.unmarshalPersonne(Personne.java:171)
at src.castorTest.main(castorTest.java:31)
 
I really don't know how to correct this problem, i guess it's related with this XML containing more than one namespace ...
If anyone can help me solving this problem, i would be really pleased.
 
Regards
BQ.
 

Reply via email to