Using the following code, I can successfully add a subElement where I want, and functionally it works. For readability, I want to format the way item.append
inserts the new sub elements.
My Code:
import xml.etree.ElementTree as ET
tree = ET.parse(file.xml)
root = tree.getroot()
for items in root.iter('items'):
item = ET.SubElement(items, 'item')
item.append((ET.fromstring('<item>cat</item>')))
tree.write('output.xml')
XML File:
<interface>
<object>
<items>
<item>dog</Item>
</items>
</object>
</interface>
Expected output:
<interface>
<object>
<items>
<item>dog</item>
<item>cat</item>
</items>
</object>
</interface>
Actual output:
<interface>
<object>
<items>
<item>dog</item>
<item><item>cat</item></item></items>
</object>
</interface>
Usually I wouldn't care about how it formats the output.xml file. However, I will be adding <item>
subElements in a while
loop, so when I have 4 or 5 additions, the code will get a little sloppy for readability's sake.
I have looked at a lot of similar questions concerning this, but they are either unanswered, or don't apply specifically to what I am trying to do.
Here is my code in the while loop, just incase it will add more clarification:
import xml.etree.ElementTree as ET
tree = ET.parse(file.xml)
root = tree.getroot()
while True:
item_add = input("Enter item to add: 'n")
item_string = '<item>'
item_string += item_add
item_string += '</item>'
for items in root.iter('items'):
item = ET.SubElement(items, 'item')
item.append((ET.fromstring(item_string)))
tree.write('output.xml')
#Code asking for more input, if none break out of loop
I appreciate any help in advance.