struts-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Paula Young" <pyo...@invertix.com>
Subject Digester help needed !
Date Fri, 05 Oct 2001 14:52:23 GMT
  [ fyi - I posted this problem on soap-user yesterday, but think it's more
geared toward soap-dev, so I apologize if some of you see it twice!  ]

 I've been prototyping some XML parsing logic, trying out the Apache
Digester utility, org.apache.struts.digester, and it isn't quite working as
expected.  My objective in using the digester is to parse the XML string
that comes back from the SDK and create a javabean from this data, so that
the JSP can use some nice taglibs to build the page from the bean.  I
documented my findings (because it's hard to remember all this stuff when
you're comparing different ways of doing something!) And here's my findings:

if the XML format is (as most, if not all, of my XML strings are!) :
<loginerr>
<errnum>18</errnum>
<errdesc>Your smallcorp site login has expired!!</errdesc>
</loginerr>


And my bean is :
public class ErrResp implements Serializable {
   private int errnum = 0;
   private String errdesc = null;

    // -- integer code for the error
   public int getErrnum() {
      return errnum;
   }

   public void setErrnum(int errnum) {
      this.errnum = errnum;
   }


   // -- description of error
   public String getErrdesc() {
      return errdesc;
   }

   public void setErrdesc(String errdesc) {
      this.errdesc = new String(errdesc);
   }

   // -- Utils
   public String toString()  {
      StringBuffer sb = new StringBuffer("ErrResp is:\n");
      sb.append("Errnum=" + errnum + ".\n");
      sb.append("Errdesc=" + errdesc + ".\n");
      return sb.toString();
   }


}
// ----------  END BEAN ------------------

... I could not parse it as
      digester.addObjectCreate("loginerr",
          "com.wrox.pjsp2.struts.common.ErrResp");
      digester.addSetProperties("loginerr");
      digester.addSetNext("loginerr", "gotErr",
          "com.wrox.pjsp2.struts.common.ErrResp");

... I could only parse it as:
      digester.addObjectCreate("loginerr",
          "com.wrox.pjsp2.struts.common.ErrResp");
      digester.addCallMethod("loginerr/errnum", "setErrnum", 0);
      digester.addCallMethod("loginerr/errdesc", "setErrdesc", 0);
      digester.addSetNext("loginerr", "gotErr",
          "com.wrox.pjsp2.struts.common.ErrResp");


-IF,and only if- I added the following method to the bean (since it looks
like addCallMethod doesn't do any 'reflection' on the bean, it calls the
method specified with a String class value always)  :

   public void setErrnum(String errnum) {
      this.errnum = Integer.parseInt(errnum);
   }


Had the XML been formatted like:

<loginerr errnum="18"   errdesc=" Your smallcorp site login has expired!!"
/>

...then the following code would have worked:
      digester.addObjectCreate("loginerr",
          "com.wrox.pjsp2.struts.common.ErrResp");
      digester.addSetProperties("loginerr");
      digester.addSetNext("loginerr", "gotErr",
          "com.wrox.pjsp2.struts.common.ErrResp");
 
(and the special public void setErrnum(String errnum) method is not needed
in the bean, the digester uses bean reflection correctly in this case and
the string is converted to integer automatically behind the scene).

Does anyone know why I can't use the addSetProperties way of parsing the XML
string:
<loginerr>
<errnum>18</errnum>
<errdesc>Your smallcorp site login has expired!!</errdesc>
</loginerr>

? Aren't both XML formats valid?  I really want to use addSetProperties on
this XML format so that I don't have to map each "xml element-to-bean
method" one-for-one, and so that the type conversions are automatic.  That
would be ideal.

Thanks,
Paula Young

Mime
View raw message