I need help adjusting my XML file with Python and the elementTree library.
For some background, I am not a student and work in industry. I hope to save myself a great deal of manual effort by making these changes automated and typically I would have just done this in a language such as C++ that I am more familiar with. However, there is a push to use Python in my group so I am using this as both a functional and learning exercise.
Could you please correct my use of terms and understanding? I do not simply just want the code to work, but to know that my understanding of how it works is correct.
The Problem itself:
Goal: remove the sub-element "weight" from the XML file.
Using the xml code (let's just say it is called "example.xml"):
<XML_level_1 created="2014-08-19 16:55:02" userID="User@company">
<XML_level_2 manufacturer="company" number="store-25235">
<padUnits value="mm" />
<partDescription value="Part description explained here" />
<weight value="5.2" />
</XML_level_2>
</XML_level_1>
Thus far, I have the following code:
from xml.etree import ElementTree
current_xml_tree = ElementTree.parse(filename_path) # Path to example.xml
current_xml_root = current_xml_tree.getroot()
current_xml_level_2_node = current_xml_root.findall('XML_level_2')
# Extract "weight" value for later use
for weight_value_elem in current_xml_root.iter('weight'):
weight_value = weight_value_elem.get('value')
# Remove weight sub-element from XML
# -------------------------------------
# Get all nodes entitled 'weight' from element
weight_nodes = current_xml_root.findall('weight')
print weight_nodes # result is an empty list
print weight_value_elem # Location of element 'weight' is listed
for weight_node_loc in current_xml_tree.iter('weight'):
print "for-loop check : loop has been entered"
current_xml_tree.getroot().remove(weight_value_elem)
print "for-loop has been processed"
print "Weight line removed from ", filename_path
# Write changes to XML File:
current_xml_tree.write(filename_path)
I have read this helpful resource, but have reached a point where I am stuck.
Second question: What is the relation of nodes and elements in this context?
I come from a finite element background, where nodes are understood as part of an element, defining portions / corner boundaries of what creates an element. However, am I wrong in thinking the terminology is used differently here so that nodes are not a subset of elements? Are the two terms still related in a similar way?