close

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

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error ParseError: not well-formed (invalid token) using cElementTree in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How ParseError: not well-formed (invalid token) using cElementTree Error Occurs?

Today I get the following error ParseError: not well-formed (invalid token) using cElementTree in python.

How To Solve ParseError: not well-formed (invalid token) using cElementTree Error ?

  1. How To Solve ParseError: not well-formed (invalid token) using cElementTree Error ?

    To Solve ParseError: not well-formed (invalid token) using cElementTree Error None of the above fixes worked for me. The only thing that worked was to use BeautifulSoup instead of ElementTree as follows:

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

    To Solve ParseError: not well-formed (invalid token) using cElementTree Error None of the above fixes worked for me. The only thing that worked was to use BeautifulSoup instead of ElementTree as follows:

Solution 1

None of the above fixes worked for me. The only thing that worked was to use BeautifulSoup instead of ElementTree as follows:

from bs4 import BeautifulSoup
with open("data/myfile.xml") as fp:
    soup = BeautifulSoup(fp, 'xml')

Then you can search the tree as:

soup.find_all('mytag')

Solution 2

It seems to complain about \x08 you will need to escape that.

Edit:

Or you can have the parser ignore the errors using recover

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

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read