import xml.sax
xmlFile = "emails.xml"
xmlTag = "email"
class tagHandler(xml.sax.handler.ContentHandler):
def __init__(self):
self.tags = {}
def startElement(self,name, attr):
name = name.encode('ascii')
self.tags[name] = self.tags.get(name, 0) + 1
print "Tag %s = %d" % (name, self.tags.get(name))
xmlparser = xml.sax.make_parser()
tHandler = tagHandler()
xmlparser.setContentHandler(tHandler)
xmlparser.parse(xmlFile)
tags = tHandler.tags
if tags.has_key(xmlTag):
print "%s has %d <%s> nodes." % (xmlFile, tags[xmlTag], xmlTag)
|