#!/usr/bin/python # Demonstrating SAX-based parsing of XML from xml.sax import parse, SAXParseException, ContentHandler class TagInfoHandler( ContentHandler ): """Custom xml.sax.ContentHandler""" def __init__(self, tagName ): ContentHandler.__init__(self) self.tagName = tagName self.depth = 0 #spaces to indent to show structure def startElement( self, name, attributes ): #check if this is tag name we're looking for if name == self.tagName: print "\n%s<%s> started" % ( " " * self.depth, name ) self.depth += 3 print "%sAttributes: " % (" " * self.depth ) for attribute in attributes.getNames(): print "%s%s = %s" % ( " " * self.depth, attribute, attributes.getValue( attribute ) ) def endElement( self, name ) : if name == self.tagName: self.depth -= 3 print "%s ended\n" % ( " " * self.depth, name ) def characters( self, content ) : content = content.strip() if content: print content def main(): print "Beginning XML processor..." file = raw_input( "Enter a file to parse: " ) tagname = raw_input( "Enter a tag to search for: " ) try: parse ( file, TagInfoHandler( tagname ) ) except IOError, message: print "Error reading file: ", message except SAXParseException, message: print "Error parsing file: ", message if __name__ == "__main__": main()