Ozgur Balsoy wrote:
> 
> Also, you don't need to check the XML to determine what the object is. Just
> preregister your classes with Unmarshaller.
> 
>           import org.exolab.castor.xml.ClassDescriptorResolver;
>           import org.exolab.castor.xml.util.ClassDescriptorResolverImpl;
> 
>         // this part would be static and used once.
>         ClassDescriptorResolver cdr = new ClassDescriptorResolverImpl();
>         cdr.resolve(<DataObject>.class);

Make sure you register all classes that could be top-level elements....

cdr.resolve(PossibleRootElement1.class);
cdr.resolve(PossibleRootElement2.class);

>         unmarshaller = new Unmarshaller((Class)null);
>         unmarshaller.setResolver( cdr );
>         ...
>           obj = unmarshaller.unmarshal( reader );
>           if(obj instanceof <DataObject>) {
>         ...
>           else if(...


You can also implement your own version of
org.exolab.castor.xml.ClassDescriptorResolver.

> 
> Another option could be the mapping namespaces with Java packages in
> castor.properties. However, this approach didn't get any acceptance.

It's a nice idea...it just needs to be handled by the default
ClassDescriptorResolver. 

> 
> The SourceGenerator uses org.exolab....nspackages option in
> castorbuilder.properties file to map namespaces to Java packages. I thought
> it would be great to have the same feature for the Un/Marshaller framework.
> If Unmarshaller couldn't find any class mapping with its regular ways
> (mapping file, class descriptors, class resolvers, etc), it could check the
> properties file if there were any mapping there and look for classes within
> those packages.

Ozgur, I will read your original posting regarding this...

--Keith
> 
> -----Original Message-----
> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]On Behalf Of
> Scott Dunn
> Sent: Thursday, January 10, 2002 2:06 PM
> To: [EMAIL PROTECTED]
> Subject: [castor-dev] Unmarshaling an XML instance without first knowing
> the class?
> 
> I am new to Castor and I am sure this a very basic question, but it wasn't
> immediately clear to me how this may be effected, nor could I find anything
> in the docs or archives that relate to this.  The problem is as follows:
> 
> I wish to use the XML Schema based form of data binding with source
> generation and therefore have no requirement for an XML binding file.
> 
> A client will pass an 'XML instance' for one of the classes that is defined
> in the schema, my code (more accurately, Castor) will automatically
> unmarshal this into a Java object that can then be further manipulated.
> I have a base class, DataObject from which all classes defined in the schema
> will derive.
> The examples indicate that I would need to call <DataObject>.unmarshal() in
> order to get an instance of a <DataObject>, however without peeking into the
> XML instance I cannot know on what class of <DataObject> to call
> unmarshal().
> An alternative would be to construct a Unmarshaller with an instance of a
> Mapping class. However, this requires that a mapping file be provided.  Am I
> correct in saying that the XML Schema used for source generation cannot be
> used to construct a Mapping instance?
> 
> So, it would appear that my only options are:
> 1. To partially process the XML instance to determine the class and then to
> call Unmarshaller.unmarshal(<DataObject>.class, reader), or
> 
> 2. To generate an XML binding file in addition to the XML Schema and
> construct a Mapping instance using the XML binding file.
> 
> Am I missing something? My obvious preference would be for the framework to
> determine the class using the XML Schema and process it accordingly, e.g.
> 
> {
>     ...
>     Unmarshaller um = new Unmarshaller("MyXmlSchema.xml");
>     DataObject o = (DataObject)um.unmarshall(reader);
>     ...
> }
> 
> Thanks in advance for any help you can provide,
> 
> Scott.
> 
> -----------------------------------------------------------
> If you wish to unsubscribe from this mailing, send mail to
> [EMAIL PROTECTED] with a subject of:
>         unsubscribe castor-dev
> 
> -----------------------------------------------------------
> If you wish to unsubscribe from this mailing, send mail to
> [EMAIL PROTECTED] with a subject of:
>         unsubscribe castor-dev

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

Reply via email to