Castor doesn't provide access to element names...so your add method will
have to be smart:

public void addChild(Object child) {

   if (child instanceof Foo) {
     _children.put("foo", child);
   }
   else if (child instanceof Bar) {
      _children.put("bar", child);
   }
}

You could also make a small function to calculate the name, or use
Castor's built in default algorithm:

import org.exolab.castor.xml.util.DefaultNaming;

...

private DefaultNaming _naming = new DefaultNaming();

...

public void addChild(Object child) {
    _children.put(_naming.createXMLName(child), child);
}

--Keith


[EMAIL PROTECTED] wrote:
> 
> Mike,
> 
> I haven't seen this done before.  Anyone else
> seen the name of an element used as the key for a hash?
> 
> --Erik
> 
>       > -----Original Message-----
>       > From: Michael Gilfix [mailto:[EMAIL PROTECTED]]
>       > Sent: Wednesday, October 02, 2002 2:11 PM
>       > To: [EMAIL PROTECTED]
>       > Cc: Ostermueller, Erik
>       > Subject: Re: [castor-dev] Working hashmap example
>       >
>       >
>       >   Thanks so much for the reply Erik. I had your emails
>       > in the archive
>       > but was unable to find the conclusion (the mail
>       > threading doesn't
>       > seem to be working properly). This is sort of what I
>       > want and kinda
>       > off. Ideally, I'd like to be able to do something like this:
>       >
>       > <test>
>       >   <mapping>
>       >     <foo>This is a foo string</foo>
>       >     <bar>This is a bar string</bar>
>       >   </mapping>
>       > </test>
>       >
>       >   and have that populate a hash map with where
>       > hash["foo"] == 'This is
>       > a foo string', and hash["bar"] == 'This is a bar
>       > string''. In my case,
>       > I know both my target XML and what the object should
>       > look like but
>       > I'd like to have castor automate the mapping for me. Is the key
>       > tab of absolute necessity? Can the key not be the tag itself?
>       >
>       >                      -- Mike
>       >
>       > On Wed, Oct 02 @ 11:14, [EMAIL PROTECTED] wrote:
>       > > I got this to work with the latest CVS.  It fails
>       > with anything prior.
>       > > Here is my war story:
>       > >
>       > http://www.mail-archive.com/[email protected]/msg08826.html
>       >
>       > --
>       > Michael Gilfix
>       > [EMAIL PROTECTED]
>       >
>       > For my gpg public key:
>       > http://www.eecs.tufts.edu/~mgilfix/contact.html
>       >
> 
> -----------------------------------------------------------
> 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

Reply via email to