commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Simon Kitching <>
Subject Re: [Digester] Create an object using parent attribute
Date Fri, 25 Jun 2004 00:09:29 GMT
Hi Rob,

On Fri, 2004-06-25 at 08:30, Rob Cash wrote:
> I'm trying to figure out how I can create an object using a parent attribute
> value as the className for the child. Here's a sample XML snipet:
> <parent childClass="mypackage.childClass">
>    <child attribute1="value1" attribute2="value2"/>
>    <child attribute1="value3" attribute2="value4"/>
> </parent>
> I've created and initialized parent with the following:
>         digester.addObjectCreate("parent", "mypackage.parentClass");
>         digester.addSetProperties("parent");
> The problem comes in with creating the child. I've looked at archives of this
> news group and haven't found the right way to do this with a current version.
> I'm new to Digester and the documentation, while not bad, leaves something to be
> desired. What I'd like to do is something like this:
>         digester.addObjectCreate("parent/child", <value of
> parent.getChildClass()>);
> Do I need to create my own ObjectCreationFactory to do this?
What you are trying to achieve seems rather unusual to me. Each parent
instance created will have exactly one child object. Normally,
parent-child relations are 1:n rather than 1:1.

If this is a "side-effect" rather than your real final goal, then
perhaps you could describe what effect you are really trying to achieve.

Assuming that you have a good reason for building a 1:1 relationship
like this, you can probably achieve it by using a custom Rule class. The
problem with the xml you want to process is that you want one xml tag
(the <parent> tag) to create *two* objects simultaneously (the parent
and the child), and none of the existing rules handle this.

Creating a custom Rule instance is quite easy, and Digester is
explicitly designed to allow this. 

I think you can get the effect you describe above by doing the

Copy the FactoryCreateRule class, and modify it for your purposes. It
will be simplest if you don't make any attempt to make the new rule
"generic"; hard-wire the parent class name, etc (at least for the first

When begin() is called, the new rule should:
* create an instance of ParentClass and push it on the digester stack
* get the value of the "childClass" attribute
* create an instance of the child class
* call some method on the parent class instance to tell it
  about the child class [I presume you want this]
* push the child instance on the digester stack

When end() is called, the new rule should:
* pop 2 objects off the digester stack (the child then the parent)



To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message