I've been working with python's xml.etree.ElementTree library for a bit, mostly playing around with it. I've noticed, however, that python seems to load the file into memory and edit it there, then flush it, and continue using the unedited version. For example, if my program adds an element to an XML file, then iterates through that XML file, it won't see the addition that it made earlier.
Here's the condensed version of my code:
for adding to the file:
import xml.etree.ElementTree as ET
tree = et.parse("file.xml")
root = tree.getroot()
newel = ET.Element("element", tag=foo, )
newel.text = bar
root.append(newel)
tree.write("file.xml")
for child in root:
if child.get("tag") == foo:
print(child.text)
Am I doing something wrong, or what's a good way of 'refreshing', so to speak, the XML file in the program so when I iterate through the program with a for loop I can see my new element?
I am using python 3.5.