You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by "Graham, Ryan - WMD" <ry...@ci.denver.co.us> on 2005/12/14 00:00:48 UTC

[digester] nested element detection

Hi All,

I'm trying handle the following bit of xml but I'm having some trouble
doing so:

<log>
   <logitem timestamp="Fri Dec 09 14:16:18 MST 2005">
      <error from="...">
         <message>...</message>
      </error>
   </logitem>
   <logitem timestamp="Fri Dec 09 14:18:55 MST 2005">
      <warning from="...">
         <message>...</message>
      </warning>
   </logitem>
   <logitem timestamp="Fri Dec 09 14:30:05 MST 2005">
      <info from="...">
         <message>...</message>
      </info>
   </logitem>
</log>

I want to create an object for each logitem, set the timestamp, set the
type based on the next element (error, warning or info) and then get the
message text. I've tried to do something like this without much success:
      
digester.addObjectCreate("log/logitem", LogItem.class);
digester.addSetProperties("log/logitem", "timestamp", "timestamp");

//here is where I get stuck...
digester.addSetProperties("log/logitem/*", "type", "type");
digester.addSetNext("log/logitem ", "addLogItem");

Any help would be greatly appreciated.

Thanks,
Ryan

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


Re: [digester] nested element detection

Posted by Simon Kitching <sk...@apache.org>.
Hi Ryan,

On Tue, 2005-12-13 at 16:00 -0700, Graham, Ryan - WMD wrote:
> <log>
>    <logitem timestamp="Fri Dec 09 14:16:18 MST 2005">
>       <error from="...">
>          <message>...</message>
>       </error>
>    </logitem>
>    <logitem timestamp="Fri Dec 09 14:18:55 MST 2005">
>       <warning from="...">
>          <message>...</message>
>       </warning>
>    </logitem>
>    <logitem timestamp="Fri Dec 09 14:30:05 MST 2005">
>       <info from="...">
>          <message>...</message>
>       </info>
>    </logitem>
> </log>
> 
> I want to create an object for each logitem, set the timestamp, set the
> type based on the next element (error, warning or info) and then get the
> message text. I've tried to do something like this without much success:
>       
> digester.addObjectCreate("log/logitem", LogItem.class);
> digester.addSetProperties("log/logitem", "timestamp", "timestamp");
> 
> //here is where I get stuck...
> digester.addSetProperties("log/logitem/*", "type", "type");
> digester.addSetNext("log/logitem ", "addLogItem");
> 
> Any help would be greatly appreciated.

As you've only got a limited set of possible loglevels, I would suggest
this:
  digester.addCallMethod("log/logitem/error", 1, "setType");
  digester.addObjectParam("log/logitem/error", 0, "error");
  digester.addSetProperties("log/logitem/error");
  digester.addCallMethod("log/logitem/error/message", "setMessage");
Repeat for every possible level.

Trailing wildcards are not supported by the default digester
rule-matcher. You could get fancier by using one of the enhanced
rule-matching engines (ExtendedBaseRules or RegexRules) which do support
trailing wildcards and other stuff; see method Digester.setRules. And
you could write your own "SetLevelRule" that fires for the level
element, and calls the setLevel method on the top object on the stack
using the current element name. That will need a reasonable amount of
learning about digester though - probably not necessary here.

Hope this helps...

Regards,

Simon


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