I think I may have this figured. I can now parse data successfully with some
test documents.
Here is my Data class and my digest method.
public class Data {
private String type;
//the document contains only one size attribute if there is one
private String size;
private TreeMap fields;
public Data() {
type = null;
size = null;
fields = new TreeMap();
}
/**
* @return Returns the fields.
*/
public TreeMap getFields() {
return fields;
}
/**
* @param fields The fields to set.
*/
public void setFields(Object key, Object value) {
fields.put(key, value);
}
/**
* @return Returns the size. If a null is returned the document didn't
have a size attribute
*/
public String getSize() {
return size;
}
/**
* @param size The size to set.
*/
public void setSize(String size) {
this.size = size;
}
/**
* @return Returns the type.
*/
public String getType() {
return type;
}
/**
* @param type The type to set.
*/
public void setType(String type) {
this.type = type;
}
public String toString() {
StringBuffer returnMe = new StringBuffer();
returnMe.append("This data instance contains : \n");
returnMe.append("Type : " + type + "\n");
returnMe.append("Size (null indicates no value is document) : " +
size + "\n");
returnMe.append("The fields are : \n");
Iterator keys = fields.keySet().iterator();
while (keys.hasNext()) {
String field = (String) keys.next();
String value = (String) fields.get(field);
returnMe.append("Key: " + field + "\n");
returnMe.append("Values: " + value + "\n");
}
return returnMe.toString();
}
}
public Data digestData() throws IOException, SAXException {
digester = new Digester();
digester.setValidating(false);
digester.addObjectCreate("DATA", Data.class);
//get the type
digester.addCallMethod("DATA/DEF/TYPE", "setType", 0);
//get the size attribute
digester.addCallMethod("DATA/VERSION/ITEM", "setSize", 1);
digester.addCallParam("DATA/VERSION/ITEM", 0, "SIZE");
//add each item attributes to Data's Treemap
digester.addCallMethod("DATA/VERSION/ITEM", "setFields", 2);
digester.addCallParam("DATA/VERSION/ITEM", 0, "NAME");
digester.addCallParam("DATA/VERSION/ITEM", 1);
return (Data)digester.parse(parseMe);
}
----- Original Message -----
From: "Simon Kitching" <[EMAIL PROTECTED]>
To: "Jakarta Commons Users List" <[email protected]>
Sent: Tuesday, February 08, 2005 7:17 PM
Subject: Re: Digester Quesion
> On Tue, 2005-02-08 at 18:56 -0500, Luke Shannon wrote:
> > Thanks for your response Simon.
> >
> > The program is already mostly working with dummy data. In my previous
post I
> > mentioned I needed three things. All three come from the xml, one I am
> > already getting (using digester). The other 2 I have been hard coding.
> > Requirements have changed and now I need the actual data.
>
> In that case, I recommend updating your program to handle the new items
> but initialise that data the same way you currently initialise your
> dummy data (ie not using digester). Once you have figured out how you
> will store your data (which I expect will involve new members on an Item
> class) and got the rest of the app working with that, then I am sure we
> can help you to get Digester to populate those items from the xml.
>
> Good luck,
>
> Simon
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]