commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Simon Kitching <>
Subject Re: Problems with digester
Date Wed, 10 Mar 2004 20:35:26 GMT
On Thu, 2004-03-11 at 01:32, Diego wrote:
> Hi everyone!
> I'm using commons digester to parse a xml file like this:
> <class>
>     <property>
>         <meta attribute="field-description">data1</meta>
>         <meta attribute="field-description">data2</meta>
>     </property>
> </class>
> I'm having problems getting the body of "meta" tags. I just get "null".
> 		digester.addObjectCreate(
> 			"class/property/meta",
> 			Meta.class);
> 		digester.addSetProperties(
> 			"class/property/meta",
> 			"attribute",
> 			"type");
> 		digester.addBeanPropertySetter(
> 			"class/property/meta",
> 			"data");
> 		digester.addSetNext(
> 			"class/property/meta",
> 			"addMeta");
> In the Meta class I have defined two String fields, "data" and "type", with
> their respective getters and setters. The addMeta method is correctly defined
> in
> the Property class, but when I set a breakpoint in this method, the Meta object
> comes with the "data" field set to null. The "type" is parsed correctly.
> I think the problem is in this statement:
> 		digester.addBeanPropertySetter(
> 			"class/property/meta",
> 			"data");
> In all the digester examples I've seen, the addBeanPropertySetter is used to
> pick the body of tags nested into the "object tag", and not the object tag
> itself.
> What am I doing wrong?

Hi Diego,

I believe that your code is 100% correct. The problem is simply that the
BeanPropertySetterRule is firing *after* the SetNextRule, ie the "data"
attribute of your bean is being set after the "addMeta" call is being
made to the parent. You can verify this fairly easily with a little bit
of debugging or logging.

Digester rules get three "callbacks" during parsing:
 * begin() is called at the start of the associated xml element
 * body() is called "just before" the end of the xml element
 * end() is called when the end of the xml element is encountered

To preserve proper stack ordering, begin() and body() are called in the
order that rules were added to the digester, and end() called in reverse
order. Because SetNextRule and BeanPropertySetterRule both do their work
in the end() method, the order in which their "work" is done is
effectively in reverse order from the order they were added to the

Yes, this is somewhat non-intuitive; in fact this is probably the most
awkward feature of Digester. Still for most users it doesn't matter -
the data gets set, and objects get added to their parent. Only if the
"add to parent" rule requires a fully initialised child object at the
time the add is called does the order matter.

And in this case, you should just be able to reorder your code to:
to force the data attribute to be set before the parent's addMeta
method is called.



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

View raw message