Newbie xml.sax question

Martin von Loewis loewis at informatik.hu-berlin.de
Thu Jun 21 14:11:07 EDT 2001


gustafl at algonet.se (Gustaf Liljegren) writes:

> 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)

You shouldn't return a result from startElement; instead, you need to
store the value for until after parsing, e.g.

        self.value = attrs[self.attribute]

> 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.

Parsing does not return anything. If you've recorded the results, you
can do

parser.parse(...)
print dh.value

Regards,
Martin



More information about the Python-list mailing list