How to ask sax for the file encoding

Rob Wolfe rw at
Wed Oct 4 19:32:46 CEST 2006

"Edward K. Ream" <edreamleo at> writes:

> Can anyone tell me how the content handler can determine the encoding of the 
> file?  Can sax provide this info?

Try this:

from xml.parsers import expat

s = """<?xml version='1.0' encoding='iso-8859-1'?>
<chapter>Chapter 1</chapter>

class MyParser(object):
    def XmlDecl(self, version, encoding, standalone):
        print "XmlDecl", version, encoding, standalone
    def Parse(self, data):
        Parser = expat.ParserCreate()
        Parser.XmlDeclHandler = self.XmlDecl
        Parser.Parse(data, 1)
parser = MyParser()


More information about the Python-list mailing list