rmsagar 0 Newbie Poster

The below script adds an attribute to an searched element. I would like to add the attribute at the end or which ever location i prefer, whereas the script adds at the beginning. Also I would like to commit only the changes to the file. Please share and ponder your thoughts.

I tried both minidom and ElementTree, same result.

from xml.dom.minidom import parse
dom = parse("data.xml")
for node in dom.getElementsByTagName('Person'):
    if node.getAttribute('name') == "John":
       if "Allowance" not in node.attributes.keys():
            node.setAttribute("Allowance","Yes")
       print node.toxml()