Radhakrishnan,
I'll send you an example that I have that works for me and you can tweak it to
your needs.
--Keith
Radhakrishnan J wrote:
> Keith,
>
> I get the same exception even if I specify the collection="map" attribute
> for the field. I'm posting the code. Would be nice of any of you could tweak
> the parameters a bit and see if you can get it going ( I've alread tried a
> few things without any success ).
>
> public class CastorExperiments
> {
> public static void main(String[] args) throws Exception
> {
> Writer writer;
> String mappingString =
> "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
> "<mapping><class name=\"tavant.SimpleBean\" >\n" +
> "<description>Default mapping for class
> tavant.SimpleBean</description>\n" +
> "<map-to xml=\"simple-bean\"/>\n" +
> "<field name=\"map\" required=\"false\" auto-complete=\"false\"
> transient=\"false\" \n" +
> "direct=\"false\" collection=\"map\">\n" +
> "<bind-xml name=\"map\" node=\"element\"/>\n" +
> "</field>\n" +
> "</class>\n" +
> "</mapping>\n";
>
> System.out.println(mappingString);
> Mapping mapping = new Mapping();
> InputSource source = new InputSource(new StringReader(mappingString));
> mapping.loadMapping(source);
>
> Unmarshaller unmarshaller = new Unmarshaller(mapping);
> Marshaller marshaller = new Marshaller(writer = new StringWriter());
> marshaller.setMapping(mapping);
>
> SimpleBean simpleBean = new SimpleBean();
> HashMap map = new HashMap();
> map.put("key","value");
> map.put("key1","value");
> simpleBean.setMap(map);
>
> marshaller.marshal(simpleBean);
> System.out.println(writer.toString());
> System.out.println(unmarshaller.unmarshal(new
> StringReader(writer.toString())));
> }
> }
>
> -Radhakrishnan
>
> -----Original Message-----
> From: Keith Visco [mailto:[EMAIL PROTECTED]
> Sent: Wednesday, February 25, 2004 1:08 PM
> To: [EMAIL PROTECTED]
> Subject: Re: [castor-dev] Trouble mapping java.util.Map
>
> Radhakrishnan,
>
> You are missing the collection="map" attribute in your mapping file:
>
> Change your mapping as such:
>
> <mapping>
> <class name="tavant.SimpleBean" >
> <description>Default mapping for class
> tavant.SimpleBean</description>
> <map-to xml="simple-bean"/>
> <field name="map" type="org.exolab.castor.mapping.MapItem"
> collection="map">
> <bind-xml name="map" node="element"/>
> </field>
> </class>
> </mapping>
>
> Actually, you probably don't even need to specify the type attribute in
> this case, I think the below also works fine:
>
> <mapping>
> <class name="tavant.SimpleBean" >
> <description>Default mapping for class
> tavant.SimpleBean</description>
> <map-to xml="simple-bean"/>
> <field name="map" collection="map">
> <bind-xml name="map" node="element"/>
> </field>
> </class>
> </mapping>
>
> --Keith
>
> Radhakrishnan J wrote:
> >
> > Hi,
> >
> > I downloaded Castor XML 0.9.5.3 rc1 to try out the built-in support for
> > java.util.Map. Wrote a simple test program. The following is the output -
> >
> > The class -
> >
> > public class SimpleBean
> > {
> > private Map map;
> >
> > public Map getMap()
> > {
> > return map;
> > }
> >
> > public void setMap(Map map)
> > {
> > this.map = map;
> > }
> > }
> >
> > The mapping -
> >
> > <?xml version="1.0" encoding="UTF-8"?>
> > <mapping>
> > <class name="tavant.SimpleBean" >
> > <description>Default mapping for class tavant.SimpleBean</description>
> > <map-to xml="simple-bean"/>
> > <field name="map" required="false" direct="false" transient="false" >
> > <bind-xml name="map" node="element"/>
> > </field>
> > </class>
> > </mapping>
> >
> > The Obect - > XML transformation gives me -
> >
> > <?xml version="1.0" encoding="UTF-8"?>
> > <simple-bean>
> > <map xsi:type="java:org.exolab.castor.mapping.MapItem"
> > xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
> > <key xsi:type="java:java.lang.String">key1</key>
> > <value xsi:type="java:java.lang.String">value</value>
> > </map>
> > <map xsi:type="java:org.exolab.castor.mapping.MapItem"
> > xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
> > <key xsi:type="java:java.lang.String">key</key>
> > <value xsi:type="java:java.lang.String">value</value>
> > </map>
> > </simple-bean>
> >
> > But the XML -> Object, with the above XML as input throws the following
> > error. Ideally I would like not to make any assumptions about the types
> > of the keys and values of the map entries.
> >
> > org.xml.sax.SAXException: unable to add 'map' to <simple-bean> due to
> > the following exception:
> > >>>--- Begin Exception ---<<<
> > java.lang.IllegalArgumentException: Type conversion error: could not set
> > value of map(java.util.Map) with value of type
> > org.exolab.castor.mapping.MapItem
> > at
> >
> org.exolab.castor.mapping.loader.FieldHandlerImpl.setValue(FieldHandlerImpl.
> java:474)
> > at
> >
> org.exolab.castor.xml.UnmarshalHandler.endElement(UnmarshalHandler.java:893)
> > at
> >
> org.exolab.castor.xml.UnmarshalHandler.endElement(UnmarshalHandler.java:960)
> > at org.apache.xerces.parsers.AbstractSAXParser.endElement(Unknown
> > Source)
> > at
> >
> org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanEndElement(Unknown
> > Source)
> > at
> >
> org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatc
> her.dispatch(Unknown
> > Source)
> > at
> > org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown
> > Source)
> > at org.apache.xerces.parsers.DTDConfiguration.parse(Unknown Source)
> > at org.apache.xerces.parsers.DTDConfiguration.parse(Unknown Source)
> > at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
> > at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
> > at org.exolab.castor.xml.Unmarshaller.unmarshal(Unmarshaller.java:583)
> > at org.exolab.castor.xml.Unmarshaller.unmarshal(Unmarshaller.java:491)
> > at tavant.CastorExperiments.main(CastorExperiments.java:67)
> > >>>---- End Exception ----<<<
> >
> > at
> >
> org.exolab.castor.xml.UnmarshalHandler.endElement(UnmarshalHandler.java:926)
> > at
> >
> org.exolab.castor.xml.UnmarshalHandler.endElement(UnmarshalHandler.java:960)
> > at org.apache.xerces.parsers.AbstractSAXParser.endElement(Unknown
> > Source)
> > at
> >
> org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanEndElement(Unknown
> > Source)
> > at
> >
> org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatc
> her.dispatch(Unknown
> > Source)
> > at
> > org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown
> > Source)
> > at org.apache.xerces.parsers.DTDConfiguration.parse(Unknown Source)
> > at org.apache.xerces.parsers.DTDConfiguration.parse(Unknown Source)
> > at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
> > at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
> > at org.exolab.castor.xml.Unmarshaller.unmarshal(Unmarshaller.java:583)
> > at org.exolab.castor.xml.Unmarshaller.unmarshal(Unmarshaller.java:491)
> > at tavant.CastorExperiments.main(CastorExperiments.java:67)
> >
> > Thanks,
> > Radhakrishnan
> >
> > -----------------------------------------------------------
> > 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
-----------------------------------------------------------
If you wish to unsubscribe from this mailing, send mail to
[EMAIL PROTECTED] with a subject of:
unsubscribe castor-dev