1

I am trying to write a simple XML file using python, but am having trouble getting an unescaped ampersand in the output. My current code is below, and I am expecting the output to be "&otherFile;" not "&ampotherFile"

root=tree.getroot()
print(root.tag)
root.text="\n&otherFile;\n"
tree.write("MyElements.xml")

nc415
  • 11
  • 1
  • 1
    Will this be useful: https://stackoverflow.com/questions/4972210/escape-unescaped-characters-in-xml-with-python ? – Anshul May 24 '20 at 22:06
  • Well it's not forbidden by XML if what you're trying to write is a genuine entity reference. But most XML tree models don't allow entity references to be represented in the tree. – Michael Kay May 25 '20 at 09:31
  • ElementTree (which I presume you are using) changes all occurrences of `&` to `&`. I don't know a good way to work around this. See https://stackoverflow.com/q/61019991/407651 and https://stackoverflow.com/q/7986272/407651 – mzjn May 25 '20 at 15:36

0 Answers0