Python gives 'Not well-formed xml' error because of presence of '&' characters

SyncMaster picture SyncMaster · Nov 14, 2011 · Viewed 12.1k times · Source

I am reading an xml file using Python. But my xml file contains & characters, because of which while running my Python code, it gives the following error:

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

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

Answer

Michael Kay picture Michael Kay · Nov 14, 2011

No, you can't ignore the check. Your 'xml file' is not an XML file - to be an XML file, the ampersand would have to be escaped. Therefore, no software that is designed to read XML files will parse it without error. You need to correct the software that generated this file so that it generates proper ("well-formed") XML. All the benefits of using XML for interchange disappear entirely if people start sending stuff that isn't well-formed and people receiving it try to patch it up.