You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Erik Price <ep...@ptc.com> on 2003/03/24 17:36:59 UTC

[digester] parsing XML attributes but not as bean props

I've been reading through the Digester documentation and it appears that 
XML attributes can map nicely to JavaBean properties, but I have a 
somewhat different situation and was wondering if someone could tell me 
where I should be looking for the answer to this question:

My XML file looked like this:

         <uda>
             <name>Foundation/PDM Requirement</name>
             <value>Yes</value>
             <type>LIST</type>
         </uda>

My client code (that used Digester) looked like this:

         digester.addCallMethod("report/criteria/uda",
                                "addUserDefinedAttribute", 3);
             digester.addCallParam("report/criteria/uda/name", 0);
             digester.addCallParam("report/criteria/uda/value", 1);
             digester.addCallParam("report/criteria/uda/type", 2);

and everything was fine.  However, I would like to change my XML file to 
look like this:

         <uda type="LIST">
             <name>Foundation/PDM Requirement</name>
             <value>Yes</value>
         </uda>

Now the "uda" element has an attribute "type" with a value of "LIST", 
but rather than pass the string "LIST" to a JavaBean set method, I need 
to store the value and pass it as the 3rd argument to the 
"addUserDefinedAttribute" method.

Does anyone know how to refer to an attribute in the same fashion as 
referring to a nested element using Digester?  I did not find evidence 
that Digester documentation actually uses XPath so I am not certain that 
I can use an XPath-like pattern to refer to the attribute in an 
"addCallParam" method.


Thanks,

Erik


Re: [digester] parsing XML attributes but not as bean props

Posted by Erik Price <ep...@ptc.com>.

Simon Kitching wrote:

> XPath isn't supported, but the method
>    digester.addCallParam(pattern, index, attributeName)
> should do the job, eg
>    digester.addCallParam("uda", 2, "type");
> will pass the value of the "type" attribute on the node matched by the
> pattern "uda" as the third parameter.

That's exactly what I needed!  I must have missed that overloaded form 
of addCallParam (looking too hard for attribute-specific methods!). 
Thanks a lot Simon.


Erik


Re: [digester] parsing XML attributes but not as bean props

Posted by Simon Kitching <si...@ecnetwork.co.nz>.
On Tue, 2003-03-25 at 04:36, Erik Price wrote:

>          <uda type="LIST">
>              <name>Foundation/PDM Requirement</name>
>              <value>Yes</value>
>          </uda>
> 
> Now the "uda" element has an attribute "type" with a value of "LIST", 
> but rather than pass the string "LIST" to a JavaBean set method, I need 
> to store the value and pass it as the 3rd argument to the 
> "addUserDefinedAttribute" method.
> 
> Does anyone know how to refer to an attribute in the same fashion as 
> referring to a nested element using Digester?  I did not find evidence 
> that Digester documentation actually uses XPath so I am not certain that 
> I can use an XPath-like pattern to refer to the attribute in an 
> "addCallParam" method.

XPath isn't supported, but the method
   digester.addCallParam(pattern, index, attributeName)
should do the job, eg
   digester.addCallParam("uda", 2, "type");
will pass the value of the "type" attribute on the node matched by the
pattern "uda" as the third parameter.