On Tue, 30 Apr 2002, Vijay Arokayaraj wrote:

> Date: Tue, 30 Apr 2002 05:18:39 -0700
> From: Vijay Arokayaraj <[EMAIL PROTECTED]>
> Reply-To: Struts Users Mailing List <[EMAIL PROTECTED]>,
>      [EMAIL PROTECTED]
> To: [EMAIL PROTECTED]
> Subject: Help required on Digester class
>
> Hi all,
> I am new to struts. My question is do we have to use the Digester class to actually 
>read any of the XML files? If so how do I actually map an XML file into a class 
>object (class here represents a model) when the XML file has got multiple elements in 
>them and is not attribuite oriented . For example
>
> <?xml version="1.0"?>
>       <user>
>             <name>vijay</name>
>              <mail>[EMAIL PROTECTED]</mail>
>        </user>
>

One way to do this would be to define a Digester rule for each nested
property name:

  digester.addObjectCreate("user", "com.mycompany.MyUserClass");
  digester.addCallMethod("user/name", "setName", 0);
  digester.addCallMethod("user/mail", "setMail", 0);

The third argument of zero is a special case that says to use the body of
the matching element as the single argument.

> Following the strut example I would structure the XML as
> <?xml version="1.0"?>
>       <user name="vijay" mail="[EMAIL PROTECTED]"/>
>
> Since I may have 2 mail accounts and I may represent it in my XML as
>              <mail>[EMAIL PROTECTED]</mail>
>              <mail>[EMAIL PROTECTED]</mail>
>

While the rules above will still work (your setMail() method will get
called once per mail address), in cases like this I normally design my
bean to have an "addMail" method instead so that it's obvious what is
going on.  Then, just change the third rule above to:

  digester.addCallMethod("user/mail", "addMail", 0);

and you are all set.

> Is there something that I am missing in the digester class.
>
> If I write my own implementation that maps a XML elemets into java
> classs's am I deviating from the Struts Frame work.
>

You might also want to look at the "betwixt" package in the project
sandbox at Jakarta Commons <http://jakarta.apache.org/commons>.  It has
logic that is designed to map from XML structures to JavaBeans (using
Digester rules) and back again.

> Many thanx in advance,
>
> Vijay
>

Craig


--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to