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 07:01:14 UTC
[digester] Setting same object within parent
Hi all,
How can i do a recursive set where one object contains
many of the same object that can go any number of
levels down?
class Person{
String name;
List personList = new ArrayList();
public void addPerson(Person p){
personList.add(p);
}
<root>
<person name="bob">
<person name="joe"/>
<person name="sue">
<person name="billy">
...
</person>
</person
</person>
</root>
Thanks so much,
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] Setting same object within parent
Posted by Reid Pinchback <re...@yahoo.com>.
The class below should get you output like:
adamOrEve name=bob
adamOrEve #children=2
adamOrEve first child name=joe
adamOrEve second child name=sue
import org.apache.commons.digester.*;
import java.io.*;
import java.util.*;
public class ParentExample {
public static class Person {
String name;
List personList = new ArrayList();
public void addPerson(Person p){
personList.add(p);
}
public void setName(String name) { this.name=name;
}
public String getName() { return name; }
}
public static class Root {
Person adamOrEve;
public void addPerson(Person p){
adamOrEve=p;
}
}
Digester makeConfiguredDigester() {
Digester digester=new Digester();
digester.addObjectCreate("*/person",
Person.class);
digester.addSetProperties("*/person");
digester.addSetNext("*/person", "addPerson");
return digester;
}
public static void main(String[] args) throws
Exception {
String input="<root><person name='bob'><person
name='joe'/><person name='sue'/></person></root>";
ParentExample ex=new ParentExample();
Digester digester=ex.makeConfiguredDigester();
Root root=new Root();
digester.push(root);
digester.parse(new StringReader(input));
System.out.println("adamOrEve
name="+root.adamOrEve.name);
System.out.println("adamOrEve
#children="+root.adamOrEve.personList.size());
System.out.println("adamOrEve first child
name="+((Person)root.adamOrEve.personList.get(0)).name);
System.out.println("adamOrEve second child
name="+((Person)root.adamOrEve.personList.get(1)).name);
}
}
--- Steve Blom <st...@yahoo.com> wrote:
> Hi all,
> How can i do a recursive set where one object
> contains
> many of the same object that can go any number of
> levels down?
> class Person{
> String name;
> List personList = new ArrayList();
>
> public void addPerson(Person p){
> personList.add(p);
> }
> <root>
> <person name="bob">
> <person name="joe"/>
> <person name="sue">
> <person name="billy">
> ...
> </person>
> </person
> </person>
> </root>
>
> Thanks so much,
> 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
>
>
__________________________________
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