Thank you, Werner. I had looked at the how-to's. The XML CTF test suite provided some great guidance. I've got a 2 level HashMap working satisfactorily.
>From these explorations, it looks like my ultimate goal of being able to >marshal a HashMap of HashMaps, Arrays and Vectors might be out of reach. From >the <field> DTD it looks like any given field can only be a be 1 type of >hard-coded collection. Is this true? Alternatively, will Castor try to match to the best field it can - allowing for repeat 'value' fields within the same <class> that have different collection attributes? I'm continuing to poke around the XML CTF test suite for evidence of this and haven't seen anything so far. Thanks. -Chris > -------Original Message------- > From: Werner Guttmann <[EMAIL PROTECTED]> > Subject: Re: [castor-user] [XML] Marshalling Nested Hashmaps > Sent: Apr 25 '07 20:03 > > Christopher, > > can I take it you have had a look at > > http://www.castor.org/how-to-map-a-hashtable.html > > to get an idea about nested hashmaps/collections in general. If that > does not help, you'll find working code in the XML CTF test suite, more > particularly at > > > http://svn.castor.codehaus.org/browse/castor/castor/trunk/xmlctf/tests/MasterTestSuite/mapping/collections/Maps > > I hope this helps > > Werner > > > Christopher Spoehr wrote: > > Hello- > > > > I'm trying to marshal/unmarshal a HashMap of HashMaps (eventually it will > be a HashMap of HMs, Vectors, and Arrays, but one step at a time.) The XML > I'll eventually be working with will be given to me as one string - that's > why I'm working with the string readers/writers. > > > > Using the Castor FAQ and the archives, I've gotten Castor to marshal and > unmarshal the bean class holding the HM successfully for a single dimensional > HashMap. For the nested HM, Castor marshals acceptably but gets hung up on > the unmarshaling. Any tips would be appreciated. I'd seen in a previous > thread that Stephen Bash has had some experience with this. Are you still out > the Stephen? > > > > Holder Class: > > > > public class HashMapHolderBean { > > > > private HashMap heldHashMap; > > > > public HashMapHolderBean() > > { > > heldHashMap = new HashMap(); > > } > > > > public HashMapHolderBean(HashMap inputHashMap) > > { > > heldHashMap = inputHashMap; > > } > > > > public HashMap getHeldHashMap() { > > return heldHashMap; > > } > > > > public void setHeldHashMap(HashMap map) { > > heldHashMap = map; > > } > > } > > > > HashMapHolderBeanMapping.xml: > > <?xml version="1.0" encoding="UTF-8"?> > > <mapping> > > <class name="com.sbc.cctp.easl.web.artt.HashMapHolderBean" > auto-complete="false"> > > <description>Default mapping for class > com.sbc.cctp.easl.web.artt.HashMapHolderBean</description> > > <map-to xml="hash-map-holder-bean" /> > > <field name="heldHashMap" collection="map"> > > <bind-xml name="held-hash-map"> > > <class > name="org.exolab.castor.mapping.MapItem"> > > <field name="key" type="string"> > > <bind-xml name="key" > node="element" /> > > </field> > > <field name="value" > type="java.lang.Object"> > > <bind-xml name="value" > node="element"> > > <class > name="org.exolab.castor.mapping.MapItem" /> > > </bind-xml> > > </field> > > </class> > > </bind-xml> > > </field> > > </class> > > </mapping> > > > > Marshalling/Unmarshalling code: > > private String testMarshalling() > > { > > HashMap mapHM = new HashMap(); > > mapHM.put("test","Hello World."); > > mapHM.put("test2","Hello Universe"); > > > > HashMap level2 = new HashMap(); > > level2.put("level2_1","I'm level two"); > > > > mapHM.put("level2", level2); > > > > HashMapHolderBean hashMapHolder = new > HashMapHolderBean(mapHM); > > StringWriter stringWriter = new StringWriter(); > > > > try > > { > > > > //InputSource mappingSource = new InputSource( new > StringReader(generateMapString())); > > InputSource mappingSource = new > InputSource(ClassLoader.getSystemResource("HashMapHolderBeanMapping.xml").toString()); > > Mapping mapping = new > Mapping(AutomatedRegressionTestTool.class.getClassLoader()); > > mapping.loadMapping(mappingSource); > > > > Marshaller marshaller = new Marshaller(stringWriter); > > marshaller.setMapping(mapping); > > marshaller.marshal(hashMapHolder); > > } > > catch (Exception e) > > { > > log.error("Marshalling Error",e); > > } > > > > return stringWriter.getBuffer().toString(); > > } > > > > private HashMapHolderBean testUnMarshalling(String marshalled) > > { > > String sourceString = marshalled; > > log.info("Marshalled length: " + marshalled.length()); > > log.info("Source length: " + sourceString.length()); > > log.info(sourceString); > > > > HashMapHolderBean hmhb = new HashMapHolderBean(); > > > > try > > { > > InputSource mappingSource = new > InputSource(ClassLoader.getSystemResource("HashMapHolderBeanMapping.xml").toString()); > > Mapping mapping = new > Mapping(AutomatedRegressionTestTool.class.getClassLoader()); > > mapping.loadMapping(mappingSource); > > > > Unmarshaller unm = new Unmarshaller(mapping); > > > > hmhb = (HashMapHolderBean)unm.unmarshal(new > StringReader(sourceString)); > > } > > catch (IOException ioException) > > { > > log.error("IOException occured in testUnMarshalling() > ", ioException); > > } > > catch (MappingException mappingException) > > { > > log.error("Mapping exception occured in > testUnMarshalling() ", mappingException); > > } > > catch (Exception e) > > { > > log.error("Unmarshalling exception caught.", e); > > } > > > > return hmhb; > > > > } > > > > toString of the HM returned by testUnMarshalling (from inside of the > holder bean, of course): > > {test2=Hello Universe, [EMAIL PROTECTED], test=Hello World.} > > > > Of course I'll be happy to answer any clarification questions one may need > to ask. > > > > Chris Spoehr > > > > --------------------------------------------------------------------- > > 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 > > --------------------------------------------------------------------- To unsubscribe from this list please visit: http://xircles.codehaus.org/manage_email

