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