commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Savitsky, Alex" <>
Subject RE: [Digester] Parsing XML to a hashtable
Date Tue, 02 Mar 2004 14:47:32 GMT
I think I wasn't very clear about my problem - the Contact bean itself has a
hashmap, and "RowId" and "LastName" act as fields there. Here's the full
source just to eliminate all confusion:

public class Contact {

	// Actual bean contains 30+ fields,
	// omitted for simplicity sake
	private Map fields = new HashMap();

	// Field name constants to use with get()
	public static final String ROW_ID = "RowId";
	public static final String LAST_NAME = "LastName";

	public String get(String name) {
		return (String) fields.get(name);

	public void set(String name, String value) {
		fields.put(name, value);

	public void populate(String xml) throws Exception {

		Digester d = new Digester();

		// *********************************
		// Now what to put there for rules??
		// *********************************


		digester.parse(new StringReader(xml));


	public static void main(String[] args) throws Exception {

		String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
			"<Contact>" +
			"<RowId>1</RowId>" +
			"<LastName>1</LastName>" +

		Contact c = new Contact();


		// What I want to get is two entries in "fields" hashmap,
		// ("RowId", "1") and
		// ("LastName", "Smith")


Now, the SetProperty rule doesn't work there, because it operates on tag
attributes, not on the tag itself - again, unless I'm missing some obscure
parameter combination that make it work in a different way...

Tried CallMethod rule, too, and CallParam seem to be unable to pick up the
tag name (e.g., "RowId") as a parameter value

Any other ideas?



-----Original Message-----
From: Martin Kersten [mailto:Martin.Kersten@Student.Uni-Magdeburg.DE]
Sent: Tuesday, March 02, 2004 9:19 AM
To: Jakarta Commons Users List
Subject: Re: [Digester] Parsing XML to a hashtable


> 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
> <Contact>
> <RowId>1</RowId>
> <LastName>Smith</LastName>
> </Contact>
> Now, while the whole setup looks fairly common, it doesn't look like
> 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
> 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(..).


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

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

//add method
contacts.addContact(Contact contact) {
       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.


Martin (Kersten)

To unsubscribe, e-mail:
For additional commands, e-mail:

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message