Hello, I've used Digester a few times in the past with much success.
Mostly, I've parsed XML documents and simply grabbed the attributes,
and not the body text. I was looking through the documentation at
http://jakarta.apache.org/commons/digester/api/ the digester package
specifically. When I came across the example of parsing the web.xml
file to grab body text. I figured, hey, this should be easy.
I have an xml document that has several elements named Part. I
essentially want to parse the XML document and create a collection of
Parts. Simple, right? Well, I'm embarrased to say I can't figure out
why I only get one Part object in my resulting Hashtable when I run
the Digester. And I can't figure out why only the first Part element
in my document is the one that shows up. So I figure i'm not parsing
recursively or something.
Here's my BOMParser class:
package com.titan.bat.parsers.ise;
import com.titan.bat.util.MessageUtil;
import com.titan.bat.util.MessageResourcesKeys;
import com.titan.bat.exception.BOMParserException;
import org.apache.commons.digester.Digester;
import org.xml.sax.SAXParseException;
import org.xml.sax.SAXException;
import java.io.File;
import java.io.IOException;
import java.util.Hashtable;
import java.util.Enumeration;
/**
* @author dant
* @version $Id$
*/
public class BOMParser implements MessageResourcesKeys {
public static Hashtable getParts() {
return parts;
}
private static Hashtable parts = new Hashtable();
/**
* TODO - Change parsing to accept String, not File
*
**/
public void parse(String document) throws BOMParserException {
Digester digester = initializeParser();
//BOMParser resultsParser = null;
try {
//resultsParser = (BOMParser)digester.parse(new StringReader(document));
digester.parse(new
File("/usr/local/projects/BOMAnalysis/web/xml/psbk.xml"));
} catch (SAXParseException saxe) {
String msg =
MessageUtil.formatMessage(MSG_FULLY_INDENTED_BOM_PARSER_FAILURE);
throw new BOMParserException(msg, saxe);
} catch (SAXException saxe) {
String msg =
MessageUtil.formatMessage(MSG_FULLY_INDENTED_BOM_PARSER_FAILURE);
throw new BOMParserException(msg, saxe);
} catch (IOException ioe) {
String msg =
MessageUtil.formatMessage(MSG_FULLY_INDENTED_BOM_IO_FAILURE);
throw new BOMParserException(msg, ioe);
}
}
/**
* Initializes the parsing rules for the Apache Digester parser.
**/
public Digester initializeParser() {
// instantiate Digester and enable XML validation
Digester digester = new Digester();
digester.push(this);
digester.setErrorHandler(new BOMParserErrorHandler());
digester.setValidating(true);
// instantiate BOMParser class
//digester.addObjectCreate("product", BOMParser.class );
digester.addObjectCreate("product/component/part",
"com.titan.bat.parsers.ise.Part" );
//digester.addCallMethod("product/component/quantity",
"setQuantity", 0);
//digester.addCallMethod("product/component/refDesignators/refDes",
"addRefDes", 0);
digester.addCallMethod("product/component/part/partNumber",
"setPartNumber", 0);
digester.addCallMethod("product/component/part/partDescription",
"setPartDescription", 0);
digester.addCallMethod("product/component/part/partMaterialCode",
"setPartMaterialCode", 0);
digester.addCallMethod("product/component/part/partUM", "setPartUM", 0);
digester.addCallMethod("product/component/part/partSpecHandling/SpecCode",
"addSpecCode", 0);
digester.addSetNext("product/component/part", "addPart" );
return digester;
}
public void addPart(Part part) {
parts.put(part.getPartNumber(), part);
}
public static void main (String args[]) {
BOMParser parser = new BOMParser();
parser.initializeParser();
try {
parser.parse("foo");
} catch (BOMParserException e) {
e.printStackTrace();
}
Hashtable parts = parser.getParts();
Enumeration x = parts.keys();
while (x.hasMoreElements()) {
String partNumber = (String)x.nextElement();
System.out.println("partnumber = " + partNumber);
Part part = (Part)parts.get(partNumber);
System.out.println("PartDescription = " +
part.getPartDescription());
System.out.println("Part Material Code = " +
part.getPartMaterialCode());
System.out.println("Part UM = " + part.getPartUM());
}
}
}
I suppose I have configured the digester incorrectly in the
initialize() method.
Anyone seeing the obvious that I'm not seeing?
Thanks in advance.
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]