Luca Di Pasquale wrote:
>
> Hy Keith,
>
> it would be nice if you could help me with the following problem.
> I need to unmarshal an incoming xml, for which I have a set of classes built from
>the source generator. I can use the following code:
>
> Unmarshaller unmarshaller = new Unmarshaller(Disposition.class);
> Disposition dispo = (Disposition)unmarshaller.unmarshal(input);
>
> This is quite easy as long as I know what xml I received, or if I parse the xml
>before unmarshalling. But it also makes the code specific to the xml I receive: is
>there any way to be more generic, that is, writing a piece of code that will work
>with any xml I receive (for which I have a mapping file or classes generated by the
>source generator, of course).
Change your code to the following:
Unmarshaller unmarshaller = new Unmarshaller((Class)null);
This will force Castor to look for the class based on the root element
name.
This works well if you set a Mapping file.
For source generated class you will need to associate your root classes
as follows:
import org.exolab.castor.xml.ClassDescriptorResolver;
import org.exolab.castor.xml.util.ClassDescriptorResolverImpl;
ClassDescriptorResolver cdr = new ClassDescriptorResolverImpl;
cdr.resolve(Disposition.class);
cdr.resolve(Foo.class); // <- repeat for all possible root elements
unmarshaller.setResolver(cdr);
You can also implement your own ClassDescriptorResolver for handling
more complex issues.
Hope that helps...
--Keith
-----------------------------------------------------------
If you wish to unsubscribe from this mailing, send mail to
[EMAIL PROTECTED] with a subject of:
unsubscribe castor-dev