commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Simon Kitching <>
Subject Re: Help with Digester xmlrules to create a complex object
Date Wed, 05 Jul 2006 23:27:04 GMT
Hi Hernan,

On Wed, 2006-07-05 at 19:29 -0300, HernĂ¡n Seoane wrote:
> Hello,  I'm having some trouble with Digester when trying to create a
> "complex" object from the following xml:
> <Competition>
>        <CompetitionName>Competition</CompetitionName>
>        <PrizeOnOffer>2000</PrizeOnOffer>
>        <WinnerFirstName>Name</WinnerFirstName>
>        <WinnerLastName>LastName</WinnerLastName>
>        <WinnerTown>Town</WinnerTown>
> </Competition>
> My xmlrules are:
> <digester-rules>
>  <pattern value="Competition">
>   <object-create-rule classname="Competition" />
>   <call-method-rule pattern="CompetitionName"
> methodname="setCompetitionName" paramcount="0" />
>   <call-method-rule pattern="PrizeOnOffer" methodname="setPrizeOnOffer"
> paramcount="0" />
>   <object-create-rule classname="Contestant" />
>   <call-method-rule pattern="WinnerFirstName" methodname="setFirstName"
> paramcount="0" />
>   <call-method-rule pattern="WinnerLastName" methodname="setLastName"
> paramcount="0" />
>   <call-method-rule pattern="WinnerTown" methodname="setTown" paramcount="0"
> />
>   <set-next-rule methodname="setWinner" paramtypes="Contestant" />
>  </pattern>
> </digester-rules>
> My Competition object has a Contestant attribute, and what I'm trying to do
> is to set the Contestant to the Competition object, but it's not working, as
> the information for both objects is at the same level in the DOM structure,
> I'm receiving the following error:
> *No such accessible method: setCompetitionName() on object:
> com.smg.copland.model.Contestant*
> The XML file I'm reading cannot be modified, so I cannot change the
> structure to nest properties. Any ideas?

Your approach isn't working because all the rules associated with the
"Competition" element fire first, creating both the Competition and the
Contestant objects and pushing them both on the stack.

Later, the nested elements are processed causing the call-method rules
to fire, and they then try to access the object on top of the stack -
the Contestant.

The easiest solution would be to add a method to your Competition class:
  public void setWinner(
    String firstName, String lastName, String town) {

    this.setWinner(new Contestant(firstName, lastName, town));
and then call this via a single call-method-rule with 3 params. You then
can avoid having an object-create-rule for the Contestant at all.

If you can't do this, then the call-method-rule has a "target offset"
method that causes it to invoke a method on the object at the specified
offset on the stack rather than the top one.



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

View raw message