commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Simon Kitching <skitch...@apache.org>
Subject Re: [Digester] parameter value from a parent node attribute
Date Sat, 22 Apr 2006 03:58:44 GMT
On Fri, 2006-04-21 at 15:23 +0200, Valerio Schiavoni wrote:
> Hello,
> i have the following xml:
> <root>
> <element id="A">
>    <subelement id="B"/>
> </element>
> </root>
> 
> whenever I match path element/subelement, i need to invoke a 2-parameters
> method  defined on the object on the stack
> ,call it twoParamMethod(String idElement,String idSubElement).
> 
> i was able to code up to this code:
> 
> digester.addCallMethod("root/element/subelement", "twoParamMethod", 2);
> //HOW DO I GET THE VALUE FOR THE FIRST PARAMETER ?

You can get the "previous object on the stack" via:
  // See addCallParam(String pattern, int paramIndex, int stackIndex)
  digester.addCallParam("root/element/subelement", 0, 1);

Assuming you have an ObjectCreateRule for <element> and <subelement>
this will pass the <element> as the 0th parameter.

However the CallMethodRule is probably not what you want; by default it
calls the object that is on the top of the stack. So in your case,
you'll end up invoking methods on the object created by the
ObjectCreateRule for <subelement>. Maybe you want SetTopRule? Or maybe
you want one of the CallMethodRule constructors that take a targetOffset
parameter (NB: these don't have factory methods on Digester because they
are only rarely needed).

>  digester.addCallParam("root/element/subelement", 1, "id"); //value for the
> second parameter
> 
> i need to this to fill in a map (stored in the root object) key="element
> id", values="list of id-s of subelements";

Before doing any further implementation, though, I suggest you think
very carefully about your object model. When people need to start doing
such tricky things with stack offsets it usually means that the java
object model they are building doesn't correspond properly to the xml.
Often the solution is to fix the object model rather than write tricky
digester rules.

Regards,

Simon



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Mime
View raw message