You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Jens Rehpöhler <je...@rehpoehler.de> on 2001/02/09 16:11:13 UTC

Digester: How to read the body of a tag?

Hi all,

I'm trying to read the body content of a xml tag with the Digester class. By
using the standard methods from the class I only get an empty String. Here's the
example code (i justed played around with the user database from the struts
example):

Digester Code:

// this works fine
digester.addObjectCreate("database/user", "model.User");
digester.addSetProperties("database/user");
digester.addSetNext("database/user", "addUser");

// here i get only an empty string
digester.addObjectCreate("database/user/tagwithbody", "java.lang.String");
digester.addSetNext("database/user/tagwithbody", "setTagwithbdoy");


What I am missing is a method called something like

  digester.addSetBody("database/user/tagwithbody");

after the addObjectCreate method. 

The setTagwithbody method is part of the User class.


Jens


Re: Digester: How to read the body of a tag?

Posted by "Craig R. McClanahan" <Cr...@eng.sun.com>.
Jens Rehpöhler wrote:

> Hello Craig,
>
> as always you gave the conclusive hint. Perhaps you should add an example of the usage of
> this methods to the documentation. It would make things much more clearer.
>

Good idea.  I'm adding an example of using this technique to the Developer's Guide page on the
Digester module.  The example will be based on processing a web application deployment
descriptor (web.xml) file, where almost everything is passed as body content rather than XML
attributes.

Look for this in tonight's nightly build.

>
> thanks,
>
> Jens
>

Craig



Re: Digester: How to read the body of a tag?

Posted by Jens Rehpöhler <je...@rehpoehler.de>.
Hello Craig,

as always you gave the conclusive hint. Perhaps you should add an example of the usage of
this methods to the documentation. It would make things much more clearer.

thanks,

Jens

"Craig R. McClanahan" wrote:

> Jens Rehpöhler wrote:
>
> > Hello Steven,
> >
> > I think I didn't described my problem correctly. Parsing the properties of a tag works
> > well. But I want to parse the content between some tags. For example:
> >
> > <database>
> >     <user propertie="name">
> >         <text>this is the value i want to initialize my class with</text>
> >     </user>
> > </database>
> >
> > With the addSetProperties method I can parse the propertie of the user tag, but I
> > can't parse the content between the <text> tag.
>
> The rules that are *supposed* to work for this would be:
>
>     digester.addCallMethod("database/user/text", "setText", 1);
>     digester.addCallParam("database/user/text", 0);
>
> Translated into English, these rules state that, whenever the specified pattern is
> encountered:
> * Set up a call to the setText(String text) method of the
>   object on the top of the call stack
> * Set the zeroth parameter (i.e. the only one) to the
>   body content of this element
>
> With other variations of the addCallParam() method, you can take parameter values from
> certain attributes of this element as well.
>
> Craig McClanahan




Re: Digester: How to read the body of a tag?

Posted by "Craig R. McClanahan" <Cr...@eng.sun.com>.
martin.cooper@tumbleweed.com wrote:

> I've been using a slightly simpler method than Craig's to get the body
> content. I use this:
>
> digester.addCallMethod("database/user/text", "setText", 0);
>
> I got the idea for this from the JavaDoc comment for addCallMethod, which
> says that specifying zero for the parameter count means "a single parameter
> from the body of this element".
>
> I don't know if there's any practical difference, other than being one line
> of code instead of two. :-)
>

50% reduction in code length, without compromising readability, is usually a good
thing :-).

Actually, I had not scrolled down far enough in the sources, while answering the
previous message, to remember that I had added this short cut.

Craig



Re: Digester: How to read the body of a tag?

Posted by ma...@tumbleweed.com.
I've been using a slightly simpler method than Craig's to get the body 
content. I use this:

digester.addCallMethod("database/user/text", "setText", 0);

I got the idea for this from the JavaDoc comment for addCallMethod, which 
says that specifying zero for the parameter count means "a single parameter 
from the body of this element".

I don't know if there's any practical difference, other than being one line 
of code instead of two. :-)

--
Martin Cooper
Tumbleweed Communications


At 01:40 PM 2/9/01 -0800, Craig R. McClanahan wrote:
>Jens Rehpöhler wrote:
>
> > Hello Steven,
> >
> > I think I didn't described my problem correctly. Parsing the properties 
> of a tag works
> > well. But I want to parse the content between some tags. For example:
> >
> > <database>
> >     <user propertie="name">
> >         <text>this is the value i want to initialize my class with</text>
> >     </user>
> > </database>
> >
> > With the addSetProperties method I can parse the propertie of the user 
> tag, but I
> > can't parse the content between the <text> tag.
>
>The rules that are *supposed* to work for this would be:
>
>     digester.addCallMethod("database/user/text", "setText", 1);
>     digester.addCallParam("database/user/text", 0);
>
>Translated into English, these rules state that, whenever the specified 
>pattern is
>encountered:
>* Set up a call to the setText(String text) method of the
>   object on the top of the call stack
>* Set the zeroth parameter (i.e. the only one) to the
>   body content of this element
>
>With other variations of the addCallParam() method, you can take parameter 
>values from
>certain attributes of this element as well.
>
>Craig McClanahan



Re: Digester: How to read the body of a tag?

Posted by "Craig R. McClanahan" <Cr...@eng.sun.com>.
Jens Rehpöhler wrote:

> Hello Steven,
>
> I think I didn't described my problem correctly. Parsing the properties of a tag works
> well. But I want to parse the content between some tags. For example:
>
> <database>
>     <user propertie="name">
>         <text>this is the value i want to initialize my class with</text>
>     </user>
> </database>
>
> With the addSetProperties method I can parse the propertie of the user tag, but I
> can't parse the content between the <text> tag.

The rules that are *supposed* to work for this would be:

    digester.addCallMethod("database/user/text", "setText", 1);
    digester.addCallParam("database/user/text", 0);

Translated into English, these rules state that, whenever the specified pattern is
encountered:
* Set up a call to the setText(String text) method of the
  object on the top of the call stack
* Set the zeroth parameter (i.e. the only one) to the
  body content of this element

With other variations of the addCallParam() method, you can take parameter values from
certain attributes of this element as well.

Craig McClanahan



Re: Digester: How to read the body of a tag?

Posted by Jens Rehpöhler <je...@rehpoehler.de>.
Hello Steven,

I think I didn't described my problem correctly. Parsing the properties of a tag works
well. But I want to parse the content between some tags. For example:

<database>
    <user propertie="name">
        <text>this is the value i want to initialize my class with</text>
    </user>
</database>

With the addSetProperties method I can parse the propertie of the user tag, but I
can't parse the content between the <text> tag.

> <database>
>  <user username="Joe">
>    <tagwithbody attribute="something">
>    </tagwithbody>
>  </user>
> </database>
>
> // rule to create instance of Tagwithbody class that is an attribute in User
> class called twb
> digester.addObjectCreate("database/user/tagwithbody", "model.Tagwithbody",
> "twb");
>
> // this sets the attribute in Tagwithbody class
> digester.addSetProperties("database/user/tagwithbody");

_______________________________________________________
Jens Rehpoehler, email: jens@rehpoehler.de



Re: Digester: How to read the body of a tag?

Posted by "Steven D. Wilkinson" <st...@acm.org>.
It also has to do with your class structure and your xml.  
I haven't tested this, but try this....


public class User {
  private Tagwithbody twb = null;
  public void setTagwithbody(Tagwithbody theClass) { twb = theClass; }
  public Tagwithbody getTagwithbody() { return twb; }
}

public class Tagwithbody {
  private String attribute = null;
  public void setAttribute(String in) { attribute = in; }
  public String getAttribute() { return attribute; }
}

<database>
 <user username="Joe">
   <tagwithbody attribute="something">
   </tagwithbody>
 </user>
</database>

digester.addObjectCreate("database/user", "model.User");
digester.addSetProperties("database/user");
digester.addSetNext("database/user", "addUser");

// rule to create instance of Tagwithbody class that is an attribute in User
class called twb
digester.addObjectCreate("database/user/tagwithbody", "model.Tagwithbody",
"twb");

// this sets the attribute in Tagwithbody class
digester.addSetProperties("database/user/tagwithbody");

// rule to call setTagwithbody method on User class, takes type of
model.Tagwithbody
digester.addSetNext("database/user/tagwithbody",
"setTagwithbody","model.Tagwithbody");


Steve

Jens Rehpöhler wrote:
> 
> Hi all,
> 
> I'm trying to read the body content of a xml tag with the Digester class. By
> using the standard methods from the class I only get an empty String. Here's the
> example code (i justed played around with the user database from the struts
> example):
> 
> Digester Code:
> 
> // this works fine
> digester.addObjectCreate("database/user", "model.User");
> digester.addSetProperties("database/user");
> digester.addSetNext("database/user", "addUser");
> 
> // here i get only an empty string
> digester.addObjectCreate("database/user/tagwithbody", "java.lang.String");
> digester.addSetNext("database/user/tagwithbody", "setTagwithbdoy");
> 
> What I am missing is a method called something like
> 
>   digester.addSetBody("database/user/tagwithbody");
> 
> after the addObjectCreate method.
> 
> The setTagwithbody method is part of the User class.
> 
> Jens

-- 
-----------------------------------------------------------------
Steven D. Wilkinson, stevendwilkinson@acm.org