Hi. I have an xml file like this:
<tree> <category title="Item 1">item 1 text <subitem title="subitem1">subitem1 text</fileitem> <subitem title="subitem2">subitem2 text</fileitem> </category> <category title="Item 2">item 2 text <subitem title="subitem21">subitem21 text</fileitem> <subitem title="subitem22">subitem22 text</fileitem> </category> </tree>
I use ElementTree to parse this file and this works okay, however, I now need to remove an item based on its title.
I can loop through the data, but for the life of me I cant work out how to remove the item once I have matched it:
import xml.etree.cElementTree as ET ... ... ... self.xml = ET.parse(self.fpath) ... ... ... titem = "subitem21" xml = self.xml.getroot() iterator = xml.getiterator("subitem") for item in iterator: f = item text = item.attrib["title"] if text == titem: xml.remove(f)
If I use print statements I can see that I am actually finding the right item, but I just cant work out how to remove it, since it would seem that I should use either xml.remove() or item.remove(), and in either case I get an error stating that "x is not in the list", presumably saying that it cant find and delete the item that it just found!
Can anyone point me in the right direction please?