Another solution is to just let Castor know which are the possible root classes:


import org.exolab.castor.util.ClassDescriptorResolverImpl; ..

ClassDescriptorResolverImpl cdr = new ClassDescriptorResolverImpl();
cdr.resolve(com.acme.mgmt.MgmtTree.class);
cdr.resolve(com.acme.wap.WapProvisioningDoc.class);
Unmarshaller unm  = new Unmarshaller((class)null);
unm.setResolver(cdr);

Object myObject = unm.unmarshal(reader);

if (myObject instanceof MgmtTree) {
  ...
}
else if (myObject instanceof WapProvisioningDoc) {
  ...
}
else {
   // error unknown type
}


Or a third solution, if you use the latest Castor CVS source code (ie from tonight), you can simply add namespace to package mappings in the castor.properties file:


org.exolab.castor.xml.nspackages=http://acme.com/Management/2.0=com.acme.mgmt,\
    http://acme.com/wap/WAPProvising/1.0=com.acme.wap

Castor will then, based on namespace of the root element, look in the appopriate package for the right class.

I added this support a couple hours ago to fix the issue reported here:

http://castor.exolab.org/phpBB2/viewtopic.php?t=330

--Keith



[EMAIL PROTECTED] wrote:
Hi, Colin.

Why don't you simply place both doctypes in the same schema? Then you have one run on 
the source generator to get all classes, and castor at runtime should be able to 
decide what classes to instantiate.

If you cannot modify either one of the schemas, you can still create a third and 
include both the others from there.

Hiran



-----Original Message-----
From: Colin Foy [mailto:[EMAIL PROTECTED]
Sent: Thursday, November 04, 2004 1:34 PM
To: [EMAIL PROTECTED]
Subject: [castor-user] Unmarshalling multiple document types



Hi All,

I am using castor source generator to parse xml files. I used different xsd's to generate the source. When I need to parse an xml file I need to know which castor generated source to try and unmarshall the xml file to. It would be very inefficient to iterate through all types.

For example, I have 2 xml documents that I need to unmarshall which have the following castor generated source :

1) MgmtTree
2) WapProvisioningDoc

The xml documents are passed to my code via a file upload and I don't know xml document type is being passed to me.

At the moment I am attaempting to unmarshall the document to a WapProvisioningDoc and if this fails I catch the validation or marshalling exception I then attempt to unmarshall it to a MgmtTree object.

Can anyone please suggest a better approach than this?

For example, within the xml file there is a doctype which I can use to distinguish which type to marshall it to. Is it possible to marshall the xml file to a generic object to determine which doc type was used? I would then marshall the xml file to the correct castor generated source object.

Thanks in advance,

Colin Foy

--
***************************************************************
* Colin Foy                 * Email: [EMAIL PROTECTED]     *
* Software Engineer         * WWW  : www.aepona.com           *
* Aepona Ltd.,              * Phone: +44 (0)28 90 275230      *
* Interpoint Building,      * Fax  : +44 (0)28 90 269111      *
* 20-24 York Street,        *                                 *
* Belfast. BT15 1AQ         *                                 *
* N. Ireland                *                                 *
***************************************************************




----------------------------------------------------------- If you wish to unsubscribe from this mailing, send mail to
[EMAIL PROTECTED] with a subject of:
unsubscribe castor-user

Reply via email to