0

I have a function that accepts an argument called label. This label is then used to replace the content of the element "assignedNode" using ElementTree.

The function loops through all the xml files in a directory and writes the "label" argument into the element content.

My "label" argument contains and "&" symbol but when ET writes it to the xml file it appears as "&amp".

Can someone instruct me how to unescape this character and force it to write the "&" symbol?

here is my code:

def apply_label(label):

clone_path = os.getcwd()
for root, dirs, files in os.walk(clone_path):
  for f in files:
    try:
        tree = ET.ElementTree(file=f)
        root = tree.getroot()
        for assignedNode in root.iter("assignedNode"):
            assignedNode.text = label
            tree = ET.ElementTree(root)
            with open(f, "w") as a:
                tree.write(a)
    except :
       pass
Thomas
  • 1,199
  • 3
  • 13
  • 25
  • Possible duplicate of [Decode HTML entities in Python string?](http://stackoverflow.com/questions/2087370/decode-html-entities-in-python-string) – Akhil Thayyil Nov 03 '15 at 09:48
  • @AkhilThayyil No- this is not a duplicate. Many of the posts on stack overflow show how to pull out text already within XML and return an unescaped version of it. I want to push text to XML that is unescaped from the start. Or at least edit the element content to show unescaped characters in the xml file. – Thomas Nov 03 '15 at 09:54
  • & is an escape character in xml, because of that reason, ElementTree parser converts that into &amp notation – Akhil Thayyil Nov 03 '15 at 09:57
  • Doing so will make your xml file invalid. "&" is a special escape character. – zxcmehran Nov 03 '15 at 10:04
  • @zxcmehran I can assure you that this would not make my file invaild. The & is needed for a label in a xml file to act as an and opperator. – Thomas Nov 03 '15 at 10:15

1 Answers1

0

Below is the list of escape characters in XML :

https://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references

& is an escape character and ElementTree parser is designed to escape this character by default

Ref : What characters do I need to escape in XML documents?

Community
  • 1
  • 1
Akhil Thayyil
  • 9,263
  • 6
  • 34
  • 48