Remove a node
root.remove(item)
Add an empty node
root.append(ET.Element('MyNodeName'))
Replace a node with an empty node
root.remove(item)
root.append(ET.Element('Strobe'))
Edit an attribute value in a sub node
for item in root.findall('Position'):
for item2 in item.findall('MovementBlock'):
width = item2.attrib['width'] #Get existing value
item2.set('width', '10') #Set a new value
xml_string = ET.tostring(root, encoding='utf8').decode('utf8') #Output the new XML
Refresh the ElementTree object
You shouldn’t need to. If for some reason you do then you can do this:
#Get the XML as a string again after the changes we've made
ET.indent(root, space=" ", level=0) #Make the XML nice to read for humans (add indents and line breaks)
data = ET.tostring(root, encoding='utf8').decode('utf8')
#Reload the xml
tree = ET.ElementTree(ET.fromstring(data)) #Load the XML as an element tree object
root = tree.getroot() #Get root element
Feel free to comment if you can add help to this page or point out issues and solutions you have found. I do not provide support on this site, if you need help with a problem head over to stack overflow.