[XML-SIG] help getting started
Paul Tremblay
phthenry@earthlink.net
Fri, 3 May 2002 00:03:18 -0400
Hello!
I have switched from perl to python because I thought python
would be better to parse xml. However, I am having a hard time
getting started. I have a specific question below, but perhaps I
should ask where I can find documentation for parsing xml with
python.
I had considered buying the O'Reilly book on xml and python until
I looked at it at a local bookstore. The chapter on sax seems
rather short and bare of examples. I am still considering buying
it. Have other people found this book helpful?
I have also found very few tutorials on line. The two I did find,
linked to python.org, were incomplete.
That brings me to my specific quetion:
I have copied exactly the script from:
http://pyxml.sourceforge.net/topics/howto/SAX.html
I am getting this error:
Traceback (most recent call last):
File "/home/paul/paultemp/parser.test.py", line 15, in ?
class FindIssue(saxutils.DefaultHandler):
AttributeError: 'xml.sax.saxutils' module has no attribute 'DefaultHandler'
I am guessing this means that saxutils has no DefaultHandler
class, though I'm really not sure. I've included a complete copy
of the script below
Thanks in advance!
Paul
***************************************
#!/usr/bin/python
from xml.sax import saxutils
from xml.sax import make_parser
from xml.sax.handler import feature_namespaces
#from xml.sax import Contenthandler
#class docHandler(ContentHandler):
class FindIssue(saxutils.DefaultHandler):
def __init__(self, title, number):
self.search_title, self.search_number = title, number
def startElement(self, name, attrs):
# If it's not a comic element, ignore it
if name != 'comic':
return
# Look for the title and number attributes (see text)
title = attrs.get('title', None)
number = attrs.get('number', None)
if title == self.search_title and number == self.search_number:
print title, '#'+str(number), 'found'
if __name__ == '__main__':
file = "/home/paul/paultemp/test.xml"
# Create a parser
parser = make_parser()
# Tell the parser we are not interested in XML namespaces
parser.setFeature(feature_namespaces, 0)
# Create the handler
dh = FindIssue('Sandman', '62')
# Tell the parser to use our handler
parser.setContentHandler(dh)
# Parse the input
parser.parse(file)
--
************************
*Paul Tremblay *
*phthenry@earthlink.net*
************************