I want to create subelement to an element that comes next to the element country singapore here.
Suppose my test.xml file looks like this
<?xml version="1.0" encoding="UTF-8"?>
<data>
<country name="Malaysia" tst="bh">
<year>2008</year>
<gdppc>141100</gdppc>
<neighbor name="Singapore" direction="E"/>
<neighbor name="Switzerland" direction="W"/>
</country>
<district>
<A name="test">
</A>
</district>
<country name="Singapore" tst="ab">
<rank updated="yes">5</rank>
<year>2011</year>
<gdppc>59900</gdppc>
<neighbor name="Malaysia" direction="N"/>
</country>
<district>
<B name="test">
</B>
</district>
</data>
In the above example,I want to create subelement to element district but the element present above should be country "singapore". It should be
<district>
<t1 name="t1>
</t1>
<B name="test">
</B>
</district>
import xml.etree.ElementTree as et
tree = et.parse("test.xml")
root = tree.getroot()
country = root.find(".//country[@name='Singapore']")
et.subelement(country,"add new subelement")
I am able to add subelement to country element. But i couldn't take the district element below the country "singapore".
Can anyone please help me here??