Erik,
I just verified that...using the addChild method works properly:
<field name="children ... set-method="addChild" ...>
Also you had a typo in your mapping file. You were using set-name,
get-name instead of set-method, get-method.
--Keith
Keith Visco wrote:
>
> The issue is probably that the object and the key are the same, you can
> either try specifying the identity field or perhaps use your addChild
> method.
>
> --Keith
>
> Keith Visco wrote:
> >
> > Hi Erik,
> >
> > It works fine if you comment out the first 4 lines of your testParent
> > method as such:
> >
> > public static void testParent(Parent p) {
> > //Child c = p.getChild("fred_key");
> > //System.out.println("just got first child");
> > //System.out.println(" child: [" + c + "] [" + c.toString() +
> > "]");
> > //System.out.println("key: Fred_key child: [" +
> > p.getChild("fred_key").toString() + "]");
> >
> > Map kids = p.getChildren();
> > for (Iterator itr = kids.values().iterator(); itr.hasNext();) {
> > Child kid = (Child)itr.next();
> > System.out.println("Here is a child [" + kid.toString() +
> > "] key [" + kid.getKey() + "]");
> > }
> >
> > }
> >
> > --Keith
> >
> > [EMAIL PROTECTED] wrote:
> > >
> > > On the lastest cut of CVS, I'm trying to marshal/unmarshal a map, like
> > > in this post:
> > >
> > > http://www.mail-archive.com/[email protected]/msg06205.html
> > >
> > > Marshalling works great. When I unmarshall, there is no data in my
> > > object.
> > > I tried a different approach and had problems there, too.
> > > I'll send the results of Approach #2 in a separate email.
> > >
> > > Thanks in advance for looking at this.
> > >
> > > ////////// mapping.xml /////////////////////////////////////////
> > > <mapping>
> > > <class name="Parent">
> > > <field name="children" type="Child" collection="map"
> > > get-name="getChildren"
> > > set-name="setChildren">
> > > <bind-xml name="string" node="element"/>
> > > </field>
> > > </class>
> > > <class name="Child" auto-complete="true">
> > > <map-to xml="Child"/>
> > > </class>
> > > </mapping>
> > >
> > > //////// Parent.java /////////////////////////////////////////
> > > import java.util.Map;
> > > import java.util.HashMap;
> > >
> > > public class Parent {
> > >
> > > HashMap _children = new HashMap();
> > >
> > > public void addChild(Child c) {
> > >
> > > _children.put(c.getKey(), c);
> > > }
> > >
> > > public Child getChild(Object key) {
> > > return (Child) _children.get(key);
> > > }
> > >
> > > public Map getChildren() {
> > > return _children;
> > > }
> > > public void setChildren(Map m) {
> > > _children = (HashMap)m;
> > > }
> > > }
> > >
> > > ///////////// Child.java //////////////////////////////////
> > > public class Child {
> > > String firstName = null;
> > > Object key = null;
> > >
> > > public Child(){}
> > >
> > > public String getFirstName() {
> > > return firstName;
> > > }
> > >
> > > public void setFirstName(String strName) {
> > > firstName = strName;
> > > }
> > > public void setKey(Object objKey) {
> > > key = objKey;
> > > }
> > >
> > > public Object getKey() {
> > > return key;
> > > }
> > >
> > > public String toString() {
> > > return firstName;
> > > }
> > > }
> > >
> > > ///////////// HashTest.java //////////////////////////
> > > import java.util.HashMap;
> > > import java.util.Iterator;
> > > import java.util.Collection;
> > > import java.util.Map;
> > >
> > > import java.io.StringWriter;
> > > import java.io.StringReader;
> > >
> > > import org.exolab.castor.mapping.Mapping;
> > > import org.exolab.castor.mapping.MappingException;
> > >
> > > import org.exolab.castor.xml.Unmarshaller;
> > > import org.exolab.castor.xml.Marshaller;
> > >
> > > import java.io.IOException;
> > > import java.io.FileReader;
> > > import java.io.OutputStreamWriter;
> > >
> > > import org.xml.sax.InputSource;
> > >
> > > public class HashTest {
> > >
> > > public static void main(String[] args) {
> > >
> > > Parent p = new Parent();
> > >
> > > Child c = null;
> > > c = new Child(); c.setFirstName("Betty");
> > > c.setKey("betty_key"); p.addChild( c);
> > > c = new Child(); c.setFirstName("Wilma");
> > > c.setKey("wilma_key"); p.addChild( c );
> > > c = new Child(); c.setFirstName("Fred");
> > > c.setKey("fred_key"); p.addChild( c );
> > > c = new Child(); c.setFirstName("Barney");
> > > c.setKey("barney_key"); p.addChild( c );
> > >
> > > testParent(p);
> > > Mapping mapping = new Mapping();
> > >
> > > try {
> > > // 1. Load the mapping information from the file
> > > mapping.loadMapping( "mapping.xml" );
> > >
> > > StringWriter sw = new StringWriter();
> > > // 4. marshal the data with and print the XML in the console
> > > Marshaller marshaller = new Marshaller(sw);
> > >
> > > marshaller.setMapping(mapping);
> > > marshaller.marshal(p);
> > > System.out.println("Here is the data [" + sw.toString() + "]");
> > >
> > > StringReader sr = new StringReader(sw.toString());
> > > // 2. Unmarshal the data
> > > Unmarshaller unmar = new Unmarshaller(mapping);
> > > Parent newParent = (Parent)unmar.unmarshal(sr);
> > >
> > > ///////////////////////////////////////////////////////
> > > ///////
> > > /////// P R O B L E M
> > > ///////
> > > System.out.println("PROBLEM: Data didn't get unmarshalled!");
> > > testParent(newParent);
> > >
> > > } catch (Exception e) {
> > > System.out.println(e);
> > > }
> > > }
> > > public static void testParent(Parent p) {
> > > Child c = p.getChild("fred_key");
> > > System.out.println("just got first child");
> > > System.out.println(" child: [" + c + "] [" + c.toString() + "]");
> > > System.out.println("key: Fred_key child: [" +
> > > p.getChild("fred_key").toString() + "]");
> > >
> > > Map kids = p.getChildren();
> > >
> > > for(Iterator itr = kids.values().iterator(); itr.hasNext();) {
> > > Child kid = (Child)itr.next();
> > > System.out.println("Here is a child [" + kid.toString() + "]
> > > key [" + kid.getKey() + "]");
> > > }
> > >
> > > }
> > >
> > > }
> > >
> > > -----------------------------------------------------------
> > > 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