I've been trying to edit one specific element content in an XML which contains multiple element contents of the same name, but the "for loop" which is required to set the element attribute will always go through the entire section and change them all.
Let's say that this is my XML:
<SectionA>
<element_content attribute="device_1" type="parameter_1" />
<element_content attribute="device_2" type="parameter_2" />
</SectionA>
I am currently using ElementTree with this code which works perfectly when a certain section has element content with different names, but it does not work for such a case - where the name is the same. It will simply change all of the content's attributes to have the same value.
for element in root.iter(section):
print element
element.set(attribute, attribute_value)
How do I access a specific element content and only change that one?
Bear in mind that I have no knowledge of the currently present attributes inside the element_content section, as I am dynamically adding them to a user's request.
Edit: Thanks to @leovp I was able to work around my problem and came up with this solution:
for step in root.findall(section):
last_element = step.find(element_content+'[last()]')
last_element.set(attribute, attribute_value)
This causes the for loop to always change the last attribute in the specific nest. Since I am dynamically adding and editing lines, this makes it change the last one I have added.
Thank you.