ElementTree Issue - Search and remove elements

Tharanga Abeyseela tharanga.abeyseela at gmail.com
Wed Oct 17 21:05:42 EDT 2012


Hi Guys,

Managed to find the element from my xml feed. but i'm having an issue
when trying to remove the parent node and write the xml file.

#!/usr/bin/env python

import elementtree.ElementTree as ET

tree = ET.parse('test.xml')
root = tree.getroot()
walkAll = tree.getiterator()

for elt in walkAll:

        for x in
elt.findall('.//{http://schemas.xxxx.com/xxx/2xx/0x/xx/ixxxx}Rating'):

                if  x.text=="NC":
                        tree.remove(elt)
                else:
                    tree.write('out.xml')

i was trying to remove the element (not the element tag).

why i cant remove the elt element from root if i found the NC matching pattern.

thanks,
tharanga



More information about the Python-list mailing list