commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jason Vinson <>
Subject [digester] Parsing Attributes of a Topic Map
Date Fri, 10 Dec 2004 17:25:16 GMT
We have a consultant who is lobbying for a Topic Map format for a 
business solution.  The following is a simplified example:

    <items type="EntityName">
      <item name="FirstName" value="Joe"/>
      <item name="MiddleName" value=""/>
      <item name="LastName" value="Blow"/>
    <item name="PhoneNumber" value="123-456-7890"/>
  (any number of entities here)

I want to digest this into an object with the following format:

public class Entity {
setFirstName(String fName);
setMiddleName(String mName);
setLastName(String lName);
setPhoneNumber(String phoneNo);

So I struggled a bit and came up with this:

Digester digester = new Digester();

digester.addObjectCreate("myCheck", ArrayList.class);

digester.addObjectCreate("myCheck/entity", clazz);
digester.addRule("myCheck/entity/item", new addAttributeSetter());
digester.addRule("myCheck/entity/items/item", new addAttributeSetter());

// Add all "entities"
digester.addSetNext("myCheck/entity", "add");

theList = (List) digester.parse(new StringReader(xml));

with an inner class "addAttributeSetter" with the following:

public void begin(String namespace, String name, Attributes attribs) {
    try {
        String theVal = attribs.getValue("value");
        String theName = StringUtils.uncapitalize(attribs.getValue("name"));
        Object obj = digester.peek();
        BeanUtils.setProperty(obj, theName, theVal);
    } catch (Exception e) {

My question is this - There must be an easier way to do this  :)

It was way too easy to create this custom rule, which makes me wonder if 
there is something similar already in place that I haven't found.

Any comments or pointers are welcomed,

View raw message