ParseError: not well-formed (invalid token) using cElementTree

前端 未结 13 1085
日久生厌
日久生厌 2020-12-16 11:10

I receive xml strings from an external source that can contains unsanitized user contributed content.

The following xml string gave a ParseError in cElementTre

13条回答
  •  醉酒成梦
    2020-12-16 11:40

    I was having the same error (with ElementTree). In my case it was because of encodings, and I was able to solve it without having to use an external library. Hope this helps other people finding this question based on the title. (reference)

    import xml.etree.ElementTree as ET
    parser = ET.XMLParser(encoding="utf-8")
    tree = ET.fromstring(xmlstring, parser=parser)
    

    EDIT: Based on comments, this answer might be outdated. But this did work back when it was answered...

提交回复
热议问题