You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by "Savitsky, Alex" <al...@Kraft.com> on 2004/03/02 15:08:36 UTC

[Digester] Parsing XML to a hashtable

Hi,

I have a HashMap-based java bean that I'm trying to populate from an XML
file. Now, the only accessors java bean exposes are get(String) and
set(String, String), and XML file contains its data in body text, like that:

<Contact>
	<RowId>1</RowId>
	<LastName>Smith</LastName>
</Contact>

Now, while the whole setup looks fairly common, it doesn't look like there's
an easy way to parse it... I tried the CallMethod rule, but apparently it
can accept parameters from pretty much anywhere - from body text, tag
attribute, even the tag node up the stack - except from the tag name itself!
Am I missing something there?

Thanks in advance,

Alex

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Re: [Digester] Parsing XML to a hashtable

Posted by Martin Kersten <Ma...@Student.Uni-Magdeburg.DE>.
Hi,

> I have a HashMap-based java bean that I'm trying to populate from an XML
> file. Now, the only accessors java bean exposes are get(String) and
> set(String, String), and XML file contains its data in body text, like
that:
>
> <Contact>
> <RowId>1</RowId>
> <LastName>Smith</LastName>
> </Contact>
>
> Now, while the whole setup looks fairly common, it doesn't look like
there's
> an easy way to parse it... I tried the CallMethod rule, but apparently it
> can accept parameters from pretty much anywhere - from body text, tag
> attribute, even the tag node up the stack - except from the tag name
itself!
> Am I missing something there?
>
> Thanks in advance,
Check the addSetProperty method. Should help you, I guess. If not
compose the contact using a bean and add it to your map represented
by the next xml level tag (like <contacts>) using addSetNext(..).

Example:

   addCreateObject("*/contact", Contact.class);
   ... (initialize the contact rowId and lastName properties using
addSetProperties)
   addSetNext("*/contact","addContact");

   + top level (or next higher level).
   addCreateObject("contacts", ContactMap.class);

//add method
contacts.addContact(Contact contact) {
    if(contact.isValid())
       contactMap.set(contact.getRowId(), contact.getLastName());
}

I think you can guess the meaning of it.

Summary: Try addSetProperty rule first. If it is not working try the
second approach.


Bye,

Martin (Kersten)



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org