Search code examples
pythonxmlsax

Turn on namespace mode with Python xml.sax


Yikes, I can't seem to turn on namespace mode and get my contentHandler's startElementNS and endElementNS called. Instead it is still calling startElement and endElement. Here's how I'm setting it up:

source = open(sourceFileName)
xml_parser = xml.sax.make_parser()
handler = MyContentHandler()
xml_parser.setContentHandler(handler)
xml_parser.setFeature(xml.sax.handler.feature_namespaces, True)
xml.sax.parse(source, handler)

How do I turn on namepace mode so it calls startElementNS etc?


Solution

  • The last line (xml.sax.parse(source, handler)) creates a new parser. You are not using the xml_parser object for which you have set up namespace mode.

    It should work if you change this line to

    xml_parser.parse(source)