Hi Till,

Thanks for detailed description...I know it's ugly but you can try the
following:

<field name="..." type="org.exolab.castor.mapping.MapItem"
collection="map">
   <bind-xml>
      <class name="org.exolab.castor.mapping.MapItem">
         <field name="key" type="string"/>
         <field name="value" type="string" collection="array"/>
      </class>
   </bind-xml>
</field>

I haven't tried this yet, I'll try it in a few moments to make sure it
works...if not i'll try to come up with a workaround.

Obviously that won't work for default introspection. In that case, I
think what needs to happen is that the MapItem should "wrap" the items
of the array (or any collection) in a mutlivalued container so that the
output looks something like the following:

<key>....</key>
<value>
   <array-item>value1</array-item>
   <array-item>value2</array-item>
</value>

--Keith

[EMAIL PROTECTED] wrote:
> 
> Hi out there (especially Keith ;-) )
> 
> This mail kind of refers to a previous thread (Subject "java.util.Map
> support") where I described my problems unmarshalling the XML
> representation of a HashMap generated by castors default mapping. But
> since this starts the discussion about fixing that bug I decided to
> start a new thread.
> 
> I decided to dig into the sources of castor to find out, what goes
> wrong during unmarshalling a HashMap (see thread mentioned above). My
> starting point was the handling of a HashMap without a special mapping
> file, since in my opinion It would be best for castor to be able to
> marshall and unmarshall HashMaps containing keys and values of any
> type, without having to specify a special mapping.
> 
> Here is my assumption, why unmarshalling doesn't work:
> When not defining a custom mapping in a mapping file, castor's
> Introspector generates XMLClassDescriptor and XMLFieldDescriptor for
> the classes and their fields (In
> Introspector.generateClassDescriptor()) which have to be marshalled.
> From now on, I will refer to the "_value" field of MapItem to
> describe, what I think goes wrong. In my case the generation of the
> field descriptor for MapItem's _value field messes up the
> unmarshalling process. The getValue method of MapItem returns an
> object of type java.lang.Object. The
> Introspector.isCollection(java.lang.Object) call evalulates to false
> and the "_multivalued" flag of the field descriptor is being set to
> false as well. This causes the unmarshaller to throw a
> ValidationException in Unmarshaller.endElement(String) : 836, when it
> encounters more than one "value" elements in the XML.
> So, that's what I think where the error is.
> But I have no idea how to fix this. In my opinion the FieldDescriptor
> has to be generated depending on how the XML looks, which has to be
> unmarshalled.
> In my case: when there is more than one value element, the field
> descriptor must describe this field as multivalued and an array has to
> be built. I also used my sweet eclipse debugger to just switch the
> _multivalued flag to true. The effect was, that the unmarhsalled
> hashmap did not contian an array, but the last value element in my XML
> appeared as the only value in my HashMap element. Well, at least
> unmarshalling finished without throwing an exception ;-)
> 
> If anybody has an idea how to fix this, I would really appreciate it,
> if you could tell me. My boss wants me to have this working the day
> after tomorrow :-)
> 
> kind regards,
> Till Kothe

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

Reply via email to