commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Dan Tarkenton <>
Subject [Digester] basic usage problem. Help me understand the stack.
Date Mon, 26 Jul 2004 21:15:46 GMT
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 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.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));
        } catch (SAXParseException saxe) {
	    	String msg =
	    	throw new BOMParserException(msg, saxe);
    	} catch (SAXException saxe) {
	    	String msg =
	    	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.setErrorHandler(new BOMParserErrorHandler());

        // instantiate BOMParser class
        //digester.addObjectCreate("product", BOMParser.class );
"com.titan.bat.parsers.ise.Part" );

"setQuantity", 0);
"addRefDes", 0);
"setPartNumber", 0);
"setPartDescription", 0);
"setPartMaterialCode", 0);
        digester.addCallMethod("product/component/part/partUM", "setPartUM", 0);
"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();
        try {
        } catch (BOMParserException e) {
        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 = " +
            System.out.println("Part Material Code = " +
            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:
For additional commands, e-mail:

View raw message