Is there a way to force lxml to parse Unicode strings that specify an encoding in a tag?

Viewed 21764

I have an XML file that specifies an encoding, and I use UnicodeDammit to convert it to unicode (for reasons of storage, I can't store it as a string). I later pass it to lxml but it refuses to ignore the encoding specified in the file and parse it as Unicode, and it raises an exception.

How can I force lxml to parse the document? This behaviour seems too restrictive.

4 Answers

I had an existing implementation and I needed to have the tree. I also had a nbsp; issue in a meta tag. Setting resolve_entities to false fixed that issue.

    opener = urllib.request.build_opener()
    response = opener.open(url['url'])
    raw_page = response.read()
    response.close()
    parsed_page = raw_page.replace(b'encoding="UTF-8"',b'')
    parsed_page = StringIO(parsed_page.decode('ASCII'))
    parser = ET.XMLParser(resolve_entities = False, encoding="ASCII")
    tree = ET.parse(parsed_page, parser)
    root = tree.getroot()
Related