On Sat, 18 Sep 1999 [EMAIL PROTECTED] wrote:
> Date: Sat, 18 Sep 1999 23:48:02 +1100
> From: [EMAIL PROTECTED]
> Reply-To: Struts Developers List <[EMAIL PROTECTED]>
> To: [EMAIL PROTECTED]
> Subject: Using Digester class to poulate form data
>
>
>
> hi all,
>
> Has anybody tried using digester class? My problem is that i have
> data in a xml file which should populate the data in the form. Can
> anybody throw a light on whether can i use the digester utility to
> parse the xml file and put the data in the corresponding class file.
>
> thanks in advance for any help, gitanjali.
>
If the nested elements in your XML document matches the structure of the
JavaBeans you wish to create, this is pretty easy. In fact, Struts itself
uses this technique to process the struts-config.xml file, and turns
things like <action> elements into instances of the
org.apache.struts.action.ActionMapping bean class. Look at the
initDigester() method to see how Struts sets this up.
In your case (populating an ActionForm instance), a couple of different
strategies are possible:
* Let Digester create the bean and then populate it for you,
by including an "object create rule" and a "set properties
rule". You will also need a "set next" rule of some sort to
maintain a reference to the object created by the "object create
rule", because it will be removed from the stack at the end of
the corresponding element. (This is the approach that Struts
uses when reading struts-config.xml, because it pushes the
ActionServlet instance on the stack, and calls things like
addMapping() to register a new ActionMapping instance.
* Precreate the bean with default properties, push it on
to the Digester evaluation stack, and set up a Digester
that only has a "set properties" rule.
The most important convention is that the attribute names in your XML
elements should match the property names in your JavaBeans. That way, the
"set properties" rule can match them up automatically.
I'd start by reading the Digester documentation in the Struts User's
Guide, and play with some simple Digesters in stand-alone Java programs.
You will get the idea of how to use it pretty quickly.
Craig
>
>
>
> --
> To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>
>
>
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>