Mis a jour le 2017-12-03, 22:17

Parsing SAX

Exemple :
import xml.sax

parser = xml.sax.make_parser()
myHandler = MyHandler()
parser.setContentHandler(myHandler)
parser.parse(myFile)
  
on peut aussi faire directement : xml.sax.parse(myFile, myHandler)
Définition de la classe handler :
class MyHandler(xml.sax.ContentHandler):
    def startDocument(self):
        pass

    def startElement(self, name, attrs):
        keys = attrs.keys()
        value = attrs.getValue('myAttributeName')

    def endElement(self, name):
        pass

    def characters(self, content):
        pass
  

Copyright python-simple.com
programmer en python, tutoriel python, graphes en python, Aymeric Duclert