On Tue, 27 May 2003, C F wrote:

> Date: Tue, 27 May 2003 13:31:16 -0700 (PDT)
> From: C F <[EMAIL PROTECTED]>
> Reply-To: Jakarta Commons Users List <[EMAIL PROTECTED]>
> To: [EMAIL PROTECTED]
> 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.

>From a more serious perspective I would not recommend an XML document
structure like your "example 2" case anyway -- I much prefer that my XML
have explicit elements to correspond to the Java objects that I'm going to
be creating.  The "example 2" approach also disables the ability to define
a department that contains more than one employee, so it seems
artificially limiting.

If you're really stuck having to read "example 2" style XML (and you can't
fire whoever is forcing it on you :-), it's still possible to do this with
Digester -- but you'll need to implement your own Rule class to do the
work.  Peruse the Digester sources for the various rules and you'll get a
pretty good idea of what's necessary.

>  In both cases I want an "Employee" object to be created with a property
> set to the node text, setName("Buddy Hackett"), and then the "Employee"
> object assigned as a property to the parent "Department" object.  I
> would appreciated it if someone could whip up some sample code, as I'm
> at a loss and Digester examples are few and far between (example 2 is
> where I'm having the trouble).
>
> Thanks!!
>

Craig

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to