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><countryname="Malaysia"tst="bh"><year>2008</year><gdppc>141100</gdppc><neighbordirection="E"name="Singapore" /><neighbordirection="W"name="Switzerland" /></country><district><Aname="test"></A></district><countryname="Singapore"tst="ab"><rankupdated="yes">5</rank><year>2011</year><gdppc>59900</gdppc><neighbordirection="N"name="Malaysia" /></country><district><t1name="t1" /><Bname="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><countryname="Malaysia"tst="bh"><year>2008</year><gdppc>141100</gdppc><neighborname="Singapore"direction="E"/><neighborname="Switzerland"direction="W"/></country><districtcountry="Malaysia"><Aname="test"></A></district><countryname="Singapore"tst="ab"><rankupdated="yes">5</rank><year>2011</year><gdppc>59900</gdppc><neighborname="Malaysia"direction="N"/></country><districtcountry="Singaporse"><Bname="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><countryname="Malaysia"tst="bh"><year>2008</year><gdppc>141100</gdppc><neighborname="Singapore"direction="E"/><neighborname="Switzerland"direction="W"/><district><Aname="test"/></district></country><countryname="Singapore"tst="ab"><rankupdated="yes">5</rank><year>2011</year><gdppc>59900</gdppc><neighborname="Malaysia"direction="N"/><districtcountry="Singaporse"><Bname="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"