Demonstrating SAX-based parsing. : Sax : XML PYTHON TUTORIALS


PYTHON TUTORIALS » XML » Sax »

 

Demonstrating SAX-based parsing.


from xml.sax import parse, SAXParseException, ContentHandler

class TagInfoHandlerContentHandler ):
   def __init__self, tagName ):
      ContentHandler.__init__self )
      self.tagName = tagName
      self.depth = 

   def startElementself, name, attributes ):
      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.getValueattribute ) )

   def endElementself, name ):
      if name == self.tagName:
         self.depth -= 3
         print "%s</%s> endedn" " " * self.depth, name )

file = "text.xml"
tagName = "tagName"
   
try:
   parsefile, TagInfoHandlertagName ) )
except IOError, message:
   print "Error reading file:", message

except SAXParseException, message:
   print "Error parsing file:", message



Leave a Comment / Note


 
Verification is used to prevent unwanted posts (spam). .


PYTHON TUTORIALS

 Navioo XML
» Sax