I am using below code to remove countries of those ranks which are not present in list lis from  tes.xml and generating updated xml  output.xml after removing the countries
Xml:
tes.xml
<?xml version="1.0"?>
<data>
    <country>
        <rank updated="yes">2</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor name="Austria" direction="E"/>
        <neighbor name="Switzerland" direction="W"/>
    </country>
    <country>
        <rank updated="yes">5</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <gpc>59900</gpc>
        <neighbor name="Malaysia" direction="N"/>
    </country>
    <country>
        <rank updated="yes">69</rank>
        <year>2011</year>
        <gdppc>13600</gdppc>
        <neighbor name="Costa Rica" direction="W"/>
        <neighbor name="Colombia" direction="E"/>
    </country>
</data>
Code:
import xml.etree.ElementTree as ET
tree = ET.parse('tes.xml')
lis = ['5', '2']
root = tree.getroot()
print(root)
for country in root.findall('country'):
  rank = str(country.find('rank').text)
  print(rank)
  if rank not in lis:
    root.remove(country)
tree.write('outpu.xml')
When it is generating output i.e. output.xml then closing tag  </data> is misplaced i.e. it is not getting closed properly even though this tag is getting closed properly in tes.xml Can someone help?
Output.xml
<data>
    <country>
        <rank updated="yes">2</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor name="Austria" direction="E" />
        <neighbor name="Switzerland" direction="W" />
    </country>
    <country>
        <rank updated="yes">5</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <gpc>59900</gpc>
        <neighbor name="Malaysia" direction="N" />
    </country>
    </data>
