I need to escape special characters in an invalid XML file which is about 5000 lines long. Here’s an example of the XML that I have to deal with:
<root>
<element>
<name>name & surname</name>
<mail>name@name.org</mail>
</element>
</root>
Here the problem is the character “&” in the name. How would you escape special characters like this with a Python library? I didn’t find a way to do it with BeautifulSoup.
If you don’t care about invalid characters in the xml you could use XML parser’s
recoveroption (see Parsing broken XML with lxml.etree.iterparse):Output