You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Paula Young <py...@invertix.com> on 2001/10/04 23:09:42 UTC

RE: Digester help needed ! -- peculiar

I'm reposting to fix the typo "public void setgetErrdesc...".  text below is
corrected.  I'm still having the same parsing problem as before.

You know, if I reformat the XML string to look like this:
<loginerr errnum="18"   errdesc=" Your smallcorp site login has expired!!"
/>

...it works fine (I can create my bean using the digester).

The question is why doesn't work for the XML formatted as:
		<loginerr>
		<errnum>18</errnum>
		<errdesc>Your smallcorp site login has expired!!</errdesc>
            </loginerr>

Aren't both xml formats correct?  Are these Digester peculiarities, or SAX?

I have tons of XML I need to parse that have this latter format and am
desperate to make this work!!

Thanks,  Paula Young

		 -----Original Message-----
		From: 	Paula Young [mailto:pyoung@invertix.com] 
		Sent:	Thursday, October 04, 2001 1:34 PM
		To:	STRUTS-USER@jakarta.apache.org
		Subject:	Digester help needed !

		I need some basic DIGESTER help!  I think I'm trying to do
something simple, but it's not working for me and I've tried several things,
etc,etc,etc...  I just want to parse  an XML string that looks like:
		<loginerr>
		<errnum>18</errnum>
		<errdesc>Your smallcorp site login has expired!!</errdesc>
		</loginerr>

		My ErrResp.java bean that should result in the parse looks
like:

		package com.oaktree.struts.common;

		import java.io.Serializable;

		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 = 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();
		   }


		}

		AND my code (in the LoginAction.java ) looks like:

		     // simulate XML string return for an error.
		      StringBuffer xmlerr = new StringBuffer("");
		      xmlerr.append("<loginerr>\n");

		      xmlerr.append("<errnum>18</errnum>\n");
		      xmlerr.append("<errdesc>Your smallcorp site login has
expired!!</errdesc>\n");

		      xmlerr.append("</loginerr>\n");

		      // Use Apache digester util to parse that xml string.
		      // LoginResp bean : it contains: Away, Buddy, Group
beans
		      String xrsp = xmlerr.toString();
		      System.out.println("XML is:\n" + xrsp);
		      Digester digester = new Digester();
		      digester.push(this);
		      digester.setDebug(10);
		      digester.setValidating(false);
		      digester.addObjectCreate("loginerr",
		          "com.oaktree.struts.common.ErrResp");
		      digester.addSetProperties("loginerr");
		      digester.addSetNext("loginerr", "gotErr",
		          "com.oaktree.struts.common.ErrResp");

		      try {
		          digester.parse(new org.xml.sax.InputSource(new
StringReader(xrsp)));
		      } catch (IOException e){
		          e.printStackTrace();
		      } catch (SAXException e) {
		          e.printStackTrace();
		      }




		The 'gotErr ' that's referred to above is in the
LoginAction.java class and is simply:

		    // Method called after parsing a Loginerr XML Response:
		    public void gotErr(ErrResp er)
		    {
		        System.out.println("ErrResp bean:\n" +
er.toString());
		    }




		When it runs, the debug output I see in my window is:

		New com.oaktree.struts.common.ErrResp
		Set com.oaktree.struts.common.ErrResp properties
		Call com.oaktree.struts.logon.LogonAction.gotErr(ErrResp is:
		Errnum=0.
		Errdesc=null.
		)
		ErrResp bean:
		ErrResp is:
		Errnum=0.
		Errdesc=null.

		Pop com.oaktree.struts.common.ErrResp

		... I see the ErrResp object gets created, then properties
set, but when gotErr is called, the bean is empty.  I'm not understanding
why this straightforward case isn't working!  Otherwise, I think the
Digester utility is pretty slick, and I'd also be interested in the
reverse-digester utility, if any, question in "tiles / portal / digester"
message subject post.

		Paula Young