commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Reid Pinchback <rei...@yahoo.com>
Subject Re: [digester] Setting same object within parent
Date Tue, 09 Nov 2004 15:07:14 GMT
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 <steve_blom2004@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


Mime
View raw message