You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Steve Blom <st...@yahoo.com> on 2004/11/09 19:41:39 UTC
[digester] Key/Value setting (oops, forgot subject heading)
Hi,
I'm trying to put contents into a map object like so:
<people>
<person id="Jon">
...
</person>
<person id="Sue">
...
</person>
<people>
I have a person object, that i want to store in a map
with the key being the person id. Is this possible? I
think I'm close, but missing on something.
digester.setObjectCreate("people", java.util.HashMap);
digester.setObjectCreate("people/person",
bean.Person.class);
digester.setCallMethod("people/person", "put", 2);
digester.setCallParam("people/person", 0, "id");
digester.setCallParam("people/person", 1); ?
The id is set right, but the object value isn't. What
am i missing?
Thanks - Steve
__________________________________
Do you Yahoo!?
Check out the new Yahoo! Front Page.
www.yahoo.com
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org
Re: [digester] Key/Value setting (oops, forgot subject heading)
Posted by Reid Pinchback <re...@yahoo.com>.
You were close. You lost track of the fact that
there is a parameter stack and a digester stack, and
you need to make sure that "put" is called on the
correct object.
import org.apache.commons.digester.*;
import java.io.*;
import java.util.*;
public class PersonExample2 {
public static class Person {
String id;
List personList = new ArrayList();
public void addPerson(Person p){
personList.add(p);
}
public void setId(String id) { this.id=id; }
public String getId() { return id; }
public String toString() { return
"Person["+id+"]"; }
}
public static class Root {
Map people = null;
public void setPeople(Map p){
people=p;
}
}
Digester makeConfiguredDigester() {
Digester digester=new Digester();
digester.addObjectCreate("people", HashMap.class);
digester.addSetNext("people", "setPeople");
digester.addObjectCreate("*/person",
Person.class);
digester.addSetProperties("*/person");
digester.addRule("*/person", new CallMethodRule(1,
"put", 2));
digester.addCallParam("*/person", 0, "id");
digester.addCallParam("*/person", 1, true);
return digester;
}
public static void main(String[] args) throws
Exception {
String input="<people><person id='jon'/><person
id='sue'/></people>";
PersonExample2 ex=new PersonExample2();
Digester digester=ex.makeConfiguredDigester();
Root root=new Root();
digester.push(root);
digester.parse(new StringReader(input));
System.out.println("people="+root.people);
}
}
--- Steve Blom <st...@yahoo.com> wrote:
>
> Hi,
> I'm trying to put contents into a map object like
> so:
> <people>
> <person id="Jon">
> ...
> </person>
> <person id="Sue">
> ...
> </person>
> <people>
> I have a person object, that i want to store in a
> map
> with the key being the person id. Is this possible?
__________________________________
Do you Yahoo!?
Check out the new Yahoo! Front Page.
www.yahoo.com
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org