Python gives the error "malformed xml" due to the presence of the characters "&"

I am reading an xml file using Python. But my xml file contains & characters, due to which the following error appears when running my Python code:

 xml.parsers.expat.ExpatError: not well-formed (invalid token): 

Is there a way to ignore the & check in python?

+7
source share
2 answers

No, you cannot ignore the check. Your "xml file" is not an XML file - the ampersand must be escaped for the XML file. Therefore, no software designed to read XML files will parse it without errors. You need to fix the software that generated this file so that it generates the correct ("well-formed") XML. All the benefits of using XML for sharing completely disappear if people start sending things that are poorly formed and people receiving it try to fix it.

+6
source

For me, the line " <?xml version='1.0' encoding='iso-8859-1'?> " Was added before the line, because the trick.

 >>> text = '''<?xml version="1.0" encoding="iso-8859-1"?> ... <seuss><fish>red</fish><fish>blu\xe9</fish></seuss>''' >>> doc = elementtree.ElementTree.fromstring(text) 

Email this page https://mail.python.org/pipermail/tutor/2006-November/050757.html

+2
source

All Articles