Skip to content
Home » ParseError: not well-formed (invalid token) using cElementTree

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

To solve ParseError: not well-formed (invalid token) using cElementTree error follow below methods.

ERROR LOG

File "<pyshell#4>", line 1, in <module>
    ET.XML(s)
  File "<string>", line 9, in XML
ParseError: not well-formed (invalid token): line 4, column 18

How to solve ParseError: not well-formed (invalid token) using cElementTree ?

Refer the given methods to solve the issue.

Method 1:

You can instruct the parser to disregard errors by using recover.

from lxml import etree
parser = etree.XMLParser(recover=True)
etree.fromstring(xmlstring, parser=parser)

Method 2 :

I was experiencing the same problem (with ElementTree). In my situation, it was due to encodings, which I was able to handle without the usage of an additional library. I hope this helps other individuals who are searching for this question based on the title.

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

Hope the above solution works.

Also read : ImportError: No module named httplib