Java Mailing List Archive

http://www.junlu.com/

Home » user.castor »

Re: [castor-user] Unmarshalling a stream with no mappable element
should return null or throw an exception

Werner Guttmann

2012-03-15

Replies: Find Java Web Hosting

Author LoginPost Reply
Hi,

On 14.03.2012 03:37, Dumont Arnaud wrote:
> Hi all,
>
> I'm using a simple Unmarshaller to unmarshall a String into a Java
> bean, using Castor v1.2:
> Unmarshaller un = new Unmarshaller(MyObject.class);
> Mapping mapping = new Mapping();
> mapping.loadMapping(mappingFilePath);
> un.setMapping(mapping);
> MyBean myBean = un.unmarshal(new StringReader(myString));
>
> My mapping file:
> <?xml version="1.0" encoding="UTF-8"?>
> <mapping>
>  <class name="com.MyBean">
>      <map-to xml="myBean" />
>      <field name="myField" type="string" />
>      [...]
>  </class>
> </mapping>
>
> When myString contains XML data corresponding to MyBean, the
> unmarshalling works correctly.
> But when myString is an empty String or does not contain XML data
> corresponding to MyBean, I would expect myBean to be null or an
> exception to be thrown. Instead, I get a newly instantiated MyBean,
> with no fields set.
This might be worth having a look at. Can you please raise a Jira issue
over at http://jira.codehaus.org/browse/CASTOR and attach a working test
case that we can use to replay the problem.

> Is there a way to detect that no XML data corresponding to the bean
> class was found in myString (like a "required" attribute on the
> <class> tag?)?
> I couldn't find anything in the documentation or in the mail archive
> about this. Am I missing something?
There's an option named 'ignoreExtraElements', but that's not really
what you are looking for. This could be used to throw an exception if
there's XML content to be parsed/unmarshalled which is not expected.
Other than that ... there's nothing right now.

Regards
Werner Guttmann
> I could parse the String myself, looking for a<myBean> tag, but I
> would prefer the tag name to stay configurable in the Castor mapping
> file.
>
> Thanks in advance and many thanks for your great Castor library.
>
> Arnaud Dumont
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>
>

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

  http://xircles.codehaus.org/manage_email


©2008 junlu.com - Jax Systems, LLC, U.S.A.