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" <[EMAIL PROTECTED]> 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: [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
> --------------------
> 
> 
> 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.

>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]


---------------------------------
Do you Yahoo!?
Free online calendar with sync to Outlook(TM).

Reply via email to