You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by C F <ta...@yahoo.com> on 2003/05/27 22:31:16 UTC

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)?
 
Suppose, in my XML file, the following two nodes are valid....
 
example 1
--------------------
<department name="foo">
  <employee>
    Buddy Hackett
  </employee>
</department>
 
example 2
--------------------
<department name="foo">
  Buddy Hackett
</department>
 
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!!


---------------------------------
Do you Yahoo!?
The New Yahoo! Search - Faster. Easier. Bingo.

Re: Help with digester

Posted by C F <ta...@yahoo.com>.
Thanks for the helpful info.  I had actually tried pretty much what you suggested.... but as your predicted, there were some issues with that.  Anyway, I agree with you that explicit declarations would help here, but I'm trying to conform to a schema that has elements that allow mixed content (mixed="true").... the example I gave was bogus to simplify, not the real thing.
Anyway, I'll look into custom rules, and see who I can fire :)

"Craig R. McClanahan" <cr...@apache.org> wrote:


On Tue, 27 May 2003, C F wrote:

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

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
> --------------------
> 
> Buddy Hackett
> 

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.

Re: Help with digester

Posted by "Craig R. McClanahan" <cr...@apache.org>.

On Tue, 27 May 2003, C F wrote:

> Date: Tue, 27 May 2003 13:31:16 -0700 (PDT)
> From: C F <ta...@yahoo.com>
> Reply-To: Jakarta Commons Users List <co...@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.