'Adding subelement in xml with integer value to a key
I want to assign a new sub element to an existing xml using element tree.
below original xml -
<FILE>
<INSTANCE>
<A>123</A>
<B>ABC</B>
</INSTANCE>
<INSTANCE>
<A>456</A>
<B>DEF</B>
</INSTANCE>
</FILE>
Below expected xml -
<FILE>
<INSTANCE>
<A>123</A>
<B>ABC</B>
<C>0</C>
</INSTANCE>
<INSTANCE>
<A>456</A>
<B>DEF</B>
<C>0</C>
</INSTANCE>
</FILE>
I tried this -
import xml.etree.ElementTree as ET
tree = ET.parse("test.xml")
root = tree.getroot()
s= {"":"0"}
c = root.makeelement("C",s)
for child in root.findall('.//INSTANCE/A/..'):
child.append(c)
tree.write("test1.xml")
i got below output -

Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
