Hi there, just playing around with the apache digester.
If anyone can point me to a good tutorial on its usage, or can make suggestions on code changes for my small test program, I would appreciate it:
Say I have a simple class like:
I've created a small XML file like:
I have then created a small UserManager class:
The resulting output is:
A couple of questions:
[ul]
[li]I actually want to create a List of users. Do I have to create another class for this purpose, or can I configure the digester to wrap User objects in a list for me?[/li]
[li]Is the BeanPropertySetter string an XPath? The API doesn't give a clear indication of this? This might be why I am not getting dob set correctly. Or perhaps it is a date format issue. Any ideas?[/li]
[/ul]
Thanks for any advice. Neil
If anyone can point me to a good tutorial on its usage, or can make suggestions on code changes for my small test program, I would appreciate it:
Say I have a simple class like:
Code:
import java.util.Date;
public class User {
private String name;
private Date dob;
public String getName() { return name; }
public Date getDOB() { return dob; }
public void setName(String name) { this.name = name; }
public void setDOB(Date dob) { this.dob = dob; }
public String toString() {
return "[Name: "+name+", DOB: "+dob+"]";
}
}
Code:
<users>
<user dob="09-Jul-1971">
<name>Neil</name>
</user>
<user dob="02-Feb-1972">
<name>Jane</name>
</user>
</users>
Code:
import java.io.*;
import org.apache.commons.digester.Digester;
public class UserManager {
public static void read(String file) throws Exception {
Digester d = new Digester();
d.setValidating(false);
d.addObjectCreate("users/user", User.class);
d.addBeanPropertySetter("users/user/@dob", "dob");
d.addBeanPropertySetter("users/user/name", "name");
Object o = d.parse(new FileInputStream(file));
System.out.println(o.toString());
}
public static void main(String[] args) throws Exception {
read(args[0]);
}
}
Code:
[Name: Jane, DOB: null]
[ul]
[li]I actually want to create a List of users. Do I have to create another class for this purpose, or can I configure the digester to wrap User objects in a list for me?[/li]
[li]Is the BeanPropertySetter string an XPath? The API doesn't give a clear indication of this? This might be why I am not getting dob set correctly. Or perhaps it is a date format issue. Any ideas?[/li]
[/ul]
Thanks for any advice. Neil