commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From p...@shareowner.com
Subject RE: [Digester]How to populate a HashMap?
Date Wed, 18 Dec 2002 22:18:14 GMT
public class MyMap()
{
	private Map _mymap ;

	public MyMap()
	{
		_mymap = new HashMap() ;
	}

	public void addBook(Book mybook)
	{
		_mymap.put(mybook.getTitle(), mybook) ;
	}

}

public class MyDigester()
{
	public void load() throws IOException, SAXException
	{
		Digester d = new Digester()
	
		d.addObjectCreate("library", "MyMap") ;
		d.addObjectCreate("library/book", "Book") ;
		d.addBeanPropertySetter("library/book/title", "setTitle") ;
		d.addBeanPropertySetter("library/book/author", "setAuthor")
;
		d.addSetNext("library/book", "addBook") ;

		File input = new File("myxmlfile") ;
		MyMap m = (MyMap) d.parse(input) ;
	}
}

Regards,
 
 
Phillip Qin
 
"This Guy Thinks He Knows Everything"
Canadian Shareowner
121 Richmond Street W, 7th Floor
Toronto, ON M5H 2K1
(416) 595-9600 ext 291
 

-----Original Message-----
From: Baltz, Kenneth [mailto:Kbaltz@firstam.com] 
Sent: December 18, 2002 5:06 PM
To: Jakarta Commons Users List
Subject: [Digester]How to populate a HashMap?

How would I go about doing the following using Digester?

Given XML as such:
<library>
	<book>
		<title>TITLE TEXT</title>
		<author>AUTHOR TEXT</author>
	</book>
	<!-- multiple books to follow -->
	<book>
	...
	</book>
</library>

Assume there's a Book bean with title and author properties.


How can I make the digester read in the books and put them in a HashMap by
title?  I can't see how to pass the object on the top of the stack as an
argument to a method call.

K.C. Baltz


Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message