ElementTree - Issue In Appending The Subelement To An Element
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
Solution 1:
Here is how it can be done with ElementTree.
import xml.etree.ElementTree as ET
root = ET.parse("country.xml").getroot()
# A list of all children of the root element (in document order)
children = list(root)
# Find the Singapore 'country' element
sing = root.find(".//country/[@name='Singapore']")
# Get the index of the 'country' element
ix = children.index(sing)
# Find the wanted 'district' sibling element (position ix+1 in the list)
district = children[ix+1]
# Create a new 't1' element and add to 'district'
t1 = ET.Element("t1", name="t1")
district.insert(0, t1)
print(ET.tostring(root).decode("UTF-8"))
Output:
<data>
<country name="Malaysia" tst="bh">
<year>2008</year>
<gdppc>141100</gdppc>
<neighbor direction="E" name="Singapore" />
<neighbor direction="W" name="Switzerland" />
</country>
<district>
<A name="test">
</A>
</district>
<country name="Singapore" tst="ab">
<rank updated="yes">5</rank>
<year>2011</year>
<gdppc>59900</gdppc>
<neighbor direction="N" name="Malaysia" />
</country>
<district>
<t1 name="t1" /><B name="test"> <!-- New element added here -->
</B>
</district>
</data>
Solution 2:
What you are trying to attempt is not possible, as no below or above exists in XML. The data in XML is unordered. If you want to create a relation between country and district, you can ether add a Country Attribute to the District Element:
<?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 country="Malaysia">
<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 country="Singaporse">
<B name="test">
</B>
</district>
</data>
import xml.etree.ElementTree as et
tree = et.parse("test.xml")
root = tree.getroot()
district = root.find(".//district[@country='Singapore']")
et.subelement(district,"add new subelement")
Or make the district element a child of the country element:
<?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"/>
<district>
<A name="test"/>
</district>
</country>
<country name="Singapore" tst="ab">
<rank updated="yes">5</rank>
<year>2011</year>
<gdppc>59900</gdppc>
<neighbor name="Malaysia" direction="N"/>
<district country="Singaporse">
<B name="test"/>
</district>
</country>
</data>
import xml.etree.ElementTree as et
tree = et.parse("test.xml")
root = tree.getroot()
district = root.find(".//country[@name='Singapore']/district")
et.subelement(district,"add new subelement")
Edit: added python code
Post a Comment for "ElementTree - Issue In Appending The Subelement To An Element"