Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

question regarding apache digester

Status
Not open for further replies.

toolkit

Programmer
Aug 5, 2001
771
GB
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:
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+"]";
    }
}
I've created a small XML file like:
Code:
<users>
    <user dob="09-Jul-1971">
        <name>Neil</name>
    </user>
    <user dob="02-Feb-1972">
        <name>Jane</name>
    </user>
</users>
I have then created a small UserManager class:
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]);
    }
}
The resulting output is:
Code:
[Name: Jane, DOB: null]
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
 
Sussed it:
Code:
import java.io.*;
import java.text.*;
import java.util.*;
import org.apache.commons.digester.Digester;
public class UserManager {
    private static DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
    private List users = new ArrayList();
    public void read(String file) throws Exception {
        Digester d = new Digester();
        d.setValidating(false);
        d.push(this);
        d.addCallMethod("users/user", "add", 2);
        d.addCallParam("users/user/name", 0);
        d.addCallParam("users/user/date-of-birth", 1);
        d.parse(new FileInputStream(file));
        Iterator i = users.iterator();
        while (i.hasNext()) {
            System.out.println(i.next());
        }
    }
    public void add(String name, String dob) throws Exception {
        User u = new User();
        u.setName(name);
        u.setDOB(df.parse(dob));
        users.add(u);
    }
    public static void main(String[] args) throws Exception {
        UserManager mgr = new UserManager();
        mgr.read(args[0]);
    }
}
Had to modify my XML so that the date of birth is now contained in a date-of-birth element, instead of a dob attribute. The idea to push the UserManager onto the digester stack came from:

Cheers, Neil
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top