commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Craig R. McClanahan" <craig...@apache.org>
Subject Re: Help with digester
Date Tue, 27 May 2003 21:33:05 GMT


On Tue, 27 May 2003, C F wrote:

> Date: Tue, 27 May 2003 13:31:16 -0700 (PDT)
> From: C F <tacnaboyz@yahoo.com>
> Reply-To: Jakarta Commons Users List <commons-user@jakarta.apache.org>
> To: commons-user@jakarta.apache.org
> Subject: Help with digester
>
> Hello,
> Could somebody please give me an example of how I might try to accomlish the following
with Digester (I'm coding, not using the XML config)?
>

Assume the following APIs on your bean classes (among other public
methods):

    package mypackage;
    public class MyDepartment {
      public void setName(String name);
      // Call this "add" instead of "set" because departments
      // normally have more than one employee ;-)
      public void addEmployee(MyEmployee employee);
    }

    package mypackage;
    public class MyEmployee {
      public void setName(String name);
    }

> Suppose, in my XML file, the following two nodes are valid....
>
> example 1
> --------------------
> <department name="foo">
>   <employee>
>     Buddy Hackett
>   </employee>
> </department>
>

  digester.addObjectCreate("department", "mypackage.MyDepartment");
  digester.addSetProperties("department"); // Works for all properties
                                           // passed as attributes
  digester.addObjectCreate("department/employee",
                           "mypackage.MyEmployee");
  digester.addCallMethod("department/employee",
                         "setName", 0);    // 0 == use body content
  digester.addSetNext("department/employee",
                      "addEmployee", "mypackage.MyEmployee");

> example 2
> --------------------
> <department name="foo">
>   Buddy Hackett
> </department>

I've never actually tried this, but in *theory* this should work the same
as the above logic:

  digester.addObjectCreate("department", "mypackage.MyDepartment");
  digester.addSetProperties("department"); // Works for all properties
                                           // passed as attributes
  digester.addObjectCreate("department",
                           "mypackage.MyEmployee");
  digester.addCallMethod("department",
                         "setName", 0);    // 0 == use body content
  digester.addSetNext("department",
                      "addEmployee", "mypackage.MyEmployee");

However, you're probably going to have problems with the property
settings, which are going to both get fired off on the Employee object
instead of being interleaved the way that "example 1" works.

Mime
View raw message