You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Hernán Seoane <hs...@gmail.com> on 2006/07/06 00:29:24 UTC
Help with Digester xmlrules to create a complex object
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?
Thanks in advance,
Hernan Seoane
Re: Help with Digester xmlrules to create a complex object
Posted by Hernán Seoane <he...@emindit.com>.
Thank you very much, Simon!!! Using the targetoffset attribute in the
call-method-rule solved my problem.
Hernán
On 7/5/06, Simon Kitching <sk...@apache.org> wrote:
>
> 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.
>
>
> http://svn.apache.org/repos/asf/jakarta/commons/proper/digester/trunk/src/java/org/apache/commons/digester/xmlrules/digester-rules.dtd
>
> Regards,
>
> Simon
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
>
>
Re: Help with Digester xmlrules to create a complex object
Posted by Simon Kitching <sk...@apache.org>.
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.
http://svn.apache.org/repos/asf/jakarta/commons/proper/digester/trunk/src/java/org/apache/commons/digester/xmlrules/digester-rules.dtd
Regards,
Simon
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org