Help using Jakarta Commons Digester
posted 18 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Hello there,
I am using the Jakarta Commons Digester to facilitate XML-to-Object mapping in an init servlet.
I created the following:
1. myapp-rules.xml (containing rules for Digester)
2. attributes.xml (actual config file that I need to load & parse, and then place the objects in a data structure).
3. The POJO with setters and getters which holds the config file's info (after parsed through the init servlet).
4. A helper class which adds the POJOs to an ArrayList.
5. The init servlet which places myapp-rules.xml into the CLASSPATH and then loads the attributes.xml config file. This is where I think I need help on how to add it to the helper class.
myapp-rules.xml
Here's my web.xml file:
attributes.xml
Here's the the Attributes POJO:
The helper class which I want to put the AttributeBean POJO inside an ArrayList:
Here's the init servlet (this is where I need help on how to add POJO to Helper class (array list):
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URL;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import org.apache.commons.digester.Digester;
import org.apache.commons.digester.xmlrules.DigesterLoader;
import org.apache.log4j.Logger;
import edu.ucsd.security.affiliates.model.AttributeBean;
import edu.ucsd.security.affiliates.model.AttributeBeanHelper;
public class XmlConfigInitServlet extends HttpServlet {
private AttributeBean attributeBeans;
private AttributeBeanHelper itemList;
public void init() throws ServletException {
String prefix = getServletContext().getRealPath("/");
String file = getInitParameter("xml-config-file");
File xmlConfigFile = new File(prefix + file);
if (!xmlConfigFile.exists()) {
System.out.println("attributes.xml not found, "
+ xmlConfigFile.getAbsolutePath());
}
try {
// Configure Digester from XML ruleset
URL rules = getClass().getResource("./attribute-rules.xml");
Digester digester = DigesterLoader.createDigester(rules);
// Push empty List onto Digester's Stack
//List attributes = new ArrayList();
//digester.push(attributes);
itemList = new AttributeBeanHelper();
itemList.addTypes(attributeBeans);
digester.push(itemList);
// Parse the XML document
InputStream input = new FileInputStream(xmlConfigFile);
digester.parse(input);
Logger.getLogger(this.getClass()).warn("After parsing the digester.");
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
[/CODE]
For some odd reason, when my program tries to call the following XmlConfigInitServlet.itemList.addTypes(attributeBeans); I get the following exception:
Does anyone have any suggestions or tips on what I am doing wrong?
Sincerely,
JD
I am using the Jakarta Commons Digester to facilitate XML-to-Object mapping in an init servlet.
I created the following:
1. myapp-rules.xml (containing rules for Digester)
2. attributes.xml (actual config file that I need to load & parse, and then place the objects in a data structure).
3. The POJO with setters and getters which holds the config file's info (after parsed through the init servlet).
4. A helper class which adds the POJOs to an ArrayList.
5. The init servlet which places myapp-rules.xml into the CLASSPATH and then loads the attributes.xml config file. This is where I think I need help on how to add it to the helper class.
myapp-rules.xml
Here's my web.xml file:
attributes.xml
Here's the the Attributes POJO:
The helper class which I want to put the AttributeBean POJO inside an ArrayList:
Here's the init servlet (this is where I need help on how to add POJO to Helper class (array list):
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URL;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import org.apache.commons.digester.Digester;
import org.apache.commons.digester.xmlrules.DigesterLoader;
import org.apache.log4j.Logger;
import edu.ucsd.security.affiliates.model.AttributeBean;
import edu.ucsd.security.affiliates.model.AttributeBeanHelper;
public class XmlConfigInitServlet extends HttpServlet {
private AttributeBean attributeBeans;
private AttributeBeanHelper itemList;
public void init() throws ServletException {
String prefix = getServletContext().getRealPath("/");
String file = getInitParameter("xml-config-file");
File xmlConfigFile = new File(prefix + file);
if (!xmlConfigFile.exists()) {
System.out.println("attributes.xml not found, "
+ xmlConfigFile.getAbsolutePath());
}
try {
// Configure Digester from XML ruleset
URL rules = getClass().getResource("./attribute-rules.xml");
Digester digester = DigesterLoader.createDigester(rules);
// Push empty List onto Digester's Stack
//List attributes = new ArrayList();
//digester.push(attributes);
itemList = new AttributeBeanHelper();
itemList.addTypes(attributeBeans);
digester.push(itemList);
// Parse the XML document
InputStream input = new FileInputStream(xmlConfigFile);
digester.parse(input);
Logger.getLogger(this.getClass()).warn("After parsing the digester.");
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
[/CODE]
For some odd reason, when my program tries to call the following XmlConfigInitServlet.itemList.addTypes(attributeBeans); I get the following exception:
Does anyone have any suggestions or tips on what I am doing wrong?
Sincerely,
JD
| You firghten me terribly. I would like to go home now. Here, take this tiny ad: The new gardening playing cards kickstarter is now live! https://www.kickstarter.com/projects/paulwheaton/garden-cards |









