HomePage | RecentChanges | Preferences

This is an example of parsing XML into a List of LazyDynaBeans?. I plan to use it to present a "resolution list" in my web application. This is a list of possible matches displayed after the user enters a person's name.

If you're looking for the example that goes with my 1/28/05 post to commons-user, this one is similar but not exactly the same.

There doesn't seem to be a way to set a true indexed property with Digester. Without writing a custom Rule, I don't see how you'd use the setProp(int,String) method. (Which is not to say I _know_ how to write a rule that would do it...)

Example XML

  <person key="0423540">
    <preferredName>Ms. Mary J. Smith</preferredName>
      <line>545 Atlanta Ave.</line>
      <line>Normal, IL 12345</line>
  <person key="0520273">
    <preferredName>Ms. Marienne J. Krupac</preferredName>
      <line>123 State Street</line>
      <line>Phoenix, AZ 82123</line>
    <source>Friend of Alumni</source>

The code:

import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.commons.beanutils.LazyDynaBean;
import org.apache.commons.digester.*;

 * @author    Wendy Smoak
public class Main {

    *  The main program for the Main class
    * @param  args  The command line arguments
   public static void main(String[] args) {

      try {
         Digester digester = new Digester();

          *  When the 'resolution' tag is found, create an ArrayList
          *  and push it on the stack
         digester.addObjectCreate("resolution", ArrayList.class);

          *  When the 'download' tag is found, create a LazyDynaBean
          *  and push it on the stack
         digester.addObjectCreate("resolution/person", LazyDynaBean.class);

          *  Set the properties for all attributes of <person>

          *  Set the properties for all nested tags of <person>
          *  except the ones that should be ignored.
         String[] propertyNames = new String[]{"address"};
         String[] elementNames = new String[]{null};
         digester.addSetNestedProperties("resolution/person", propertyNames, elementNames);

          *  deal separately with address as it cannot be set as a
          *  simple nested property
         digester.addObjectCreate("resolution/person/address", ArrayList.class);
         digester.addCallMethod("resolution/person/address/line", "add", 1);
         digester.addCallParam("resolution/person/address/line", 0);

         int stackPosition = 1;
         int numParams = 2;
         Rule rule = new CallMethodRule(stackPosition, "set", numParams);
         digester.addRule("resolution/person/address", rule);
         //The param 0 is the literal string "address"
         digester.addObjectParam("resolution/person/address", 0, "address");
         boolean fromStack = true;
         digester.addCallParam("resolution/person/address", 1, fromStack);

//NOTE:  The '1' in the previous line might need to be a '-1'

          *  When we hit the closing </person> tag, call ArrayList.add
         digester.addSetNext("resolution/person", "add");

         File inputFile = new File(args[0]);

         List list = (List) digester.parse(inputFile);
         Iterator iter = list.iterator();
         while (iter.hasNext()) {
            System.out.println(((LazyDynaBean) iter.next()).getMap());

      } catch (Exception exc) {



The Output

E:\java\digester>ant run
Buildfile: build.xml

    [javac] Compiling 1 source file to E:\java\digester

     [java] {key=0423540, reunionClass=MD1941, preferredName=Ms. Mary J. Smith,
address=[545 Atlanta Ave., Normal, IL 12345], birthName=Brown, status=Deceased,
source=Alumnus, birthDate=05/21/1921}
     [java] {key=0520273, reunionClass=MB1971, preferredName=Ms. Marienne J. Kru
pac, address=[123 State Street, Phoenix, AZ 82123], birthName=Smithers, status=,
 source=Friend of Alumni, birthDate=05/21/1949}

Total time: 2 seconds

HomePage | RecentChanges | Preferences
This page is read-only | View other revisions
Last edited February 7, 2005 12:19 pm by wsmoak.vpia.asu.edu (diff)