commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Simon Kitching <>
Subject Re: [digester] Need to create custom Rule?
Date Thu, 09 Jun 2005 04:14:06 GMT
On Wed, 2005-06-08 at 12:53 -0700, Dan Tarkenton wrote:
> Hello all.  I have a brief question that I believe revolves around the 
> creation of a custom Rule implementation.  I have an xml pattern that I 
> need to digest/parse, but I'm unsure of how to go about this 
> programmatically.
> The pattern i'm interested in is:
> <parent>
>     <child name="a" value="b" />
>     <child name="theParamDesired" value="theValueISeek" />
> </parent>
> I essentially want to find the child element whose name attribute is 
> theParamDesired -- every time I find this attribute I want to call a 
> setValue(theValueISeek) method on some object.  I want to ignore all 
> other child elements which have different name/value attributes.  I'm 
> not all that comfortable with manipulating the stack with a custom Rule 
> implementation. 

Yes, I believe you do need to create a custom rule. Writing a custom
Rule really is very easy; it's simpler than writing custom SAX code to
achieve the same goal.

Roughly, you need this:

class MyChildHandlerRule extends Rule {
  public void begin(ns, elementName, attrs) {
    String nameAttr = attrs.get("name");
    if (nameAttr != 'theParamDesired')

    MyValueHolder obj = (MyValueHolder) digester.peek();
    String valueAttr = attrs.getValue("value");

digester.addRule("parent/child", new MyChildHandlerRule());

The SetPropertyRule does something similar to what you describe, but not
exactly. You might want to have a look at the code for this rule. But
being a "generic" rule, it can't make some of the simplifying
assumptions that the above custom Rule class can.

By the way, when posting to commons-user, please put the component name
in the subject line (as I have done in this reply).



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

View raw message