Hi,
You should use something simple, for instance try Simple from
http://simple.sourceforge.net. The following could be done cleaner and
faster, with no obscure problems and no casting required.
public class SampleDigester {
public static void main(String[] args) throws Exception {
Persister persister = new Persister();
People people = persister.read(People.class, new File("person.xml"));
for (int i = 0; i < people.size(); i++)
{
Person p = people.get(i);
System.out.println(p.getName() + "=" + p.getAge());
}
}
}
@Root(name="people")
public class People {
@ElementList(name="list", type=Person.class)
private Dictionary<Person> list;
public Person get(int index) {
return list.get(index);
}
public int size() {
return list.size();
}
}
@Root(name="person")
public Person {
@Element(name="name")
private String name;
@Element(name="age")
private int age
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
<people>
<list>
<person>
<name>Tom Higgins</name>
<age>25</age>
</person>
<person>
<name>Barney Smith</name>
<age>75</age>
</person>
<person>
<name>Susan Shields</name>
<age>53</age>
</person>
</list>
</people>
Niall
herbison wrote:
This has got to be simple!
When I run the code below the parse sees each instance of person but nothing
is read from the xml file, why?
Here is the output:
SETAGE:0
SETNAME:
SETAGE:0
SETNAME:
SETAGE:0
SETNAME:
=0
=0
=0
here is my main routine:
public class SampleDigester
{
public static void main(String [] args) throws Exception
{
List people = new ArrayList();
// Configure Digester from XML ruleset
Digester digester = new Digester();
digester.addObjectCreate("people/person", Person.class);
digester.addSetNext("people/person", "add" ,"Person");
digester.addBeanPropertySetter("people/person", "name");
digester.addBeanPropertySetter("people/person", "age");
// Push empty List onto Digester's Stack
digester.push( people );
// Parse the XML document
InputStream input = new FileInputStream( "person.xml" );
digester.parse( input );
for (int i=0;i<people.size();i++)
{
Person p = (Person)people.get(i);
System.out.println(p.getName()+"="+p.getAge());
}
}
}
Here is my bean:
import java.io.Serializable;
public class Person implements Serializable
{
private String id;
private String name;
private int age;
public Person() {}
public String getId() { return id; }
public void setId(String v) {
System.out.println("SETID:"+v);
id = v;
}
public String getName() { return name; }
public void setName(String v) {
System.out.println("SETNAME:"+v);
name = v;
}
public int getAge() { return age; }
public void setAge(int v) {
System.out.println("SETAGE:"+v);
age = v;
}
}
Here is my xml:
<people>
<person>
<name>Tom Higgins</name>
<age>25</age>
</person>
<person>
<name>Barney Smith</name>
<age>75</age>
</person>
<person>
<name>Susan Shields</name>
<age>53</age>
</person>
</people>
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]