How to ask sax for the file encoding

Rob Wolfe rw at smsnet.pl
Wed Oct 4 13:32:46 EDT 2006


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

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

Try this:

<code>
from xml.parsers import expat

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

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()
parser.Parse(s)
</code> 

-- 
HTH,
Rob



More information about the Python-list mailing list