Newbie xml.sax question

Gustaf Liljegren gustafl at algonet.se
Thu Jun 21 10:41:34 EDT 2001


I have made a config file in XML, with elements carrying attributes with 
names and values to store parameters. The idea is to be able to retrieve 
the parameters in a single way, like:

# Set up parser etc...
c = ConfigFile()
# Retrieve value
value = c.getValue(element, attribute)

I thought SAX would be the right tool for retrieving the parameter values, 
but this is the first time I use it. I started out with a class with a 
method that shall return an attribute (parameter) value, based on a search 
for the first right element with the right attribute:

from xml.sax import saxutils

class Parameters(saxutils.DefaultHandler):
  def __init__(self, element, attribute):
    self.element = element
    self.attribute = attribute
  def startElement(self, name, attrs):
    if name == self.element:
      return attrs.getValue(self.attribute)

I invoke it according to the XML howto:
		
from xml.sax import make_parser
parser = make_parser()
dh = Parameters('limits', 'max-depth')
parser.setContentHandler(dh)
print parser.parse('e:/test/config.xml')

Now what? There's no error, but I can't figure out how to acctually 
retrieve the value that the startElement method returns. The output from 
the last line is None.

Regards,

Gustaf Liljegren



More information about the Python-list mailing list