XML : How do I remove a node in xml using ElementTree in Python by serching attributes of an element?

Sample xml file:

  <root>     <child1 />     <child2 bksetname="Defult Backup Set" />     <child3 bksetname="Local Backup Set" />  </root>    

Code:

  import xml.etree.ElementTree as ET    tr = ET.parse("Scheduler.xml")  for elem in tr.iter():      print elem.tag , elem.attrib      if elem.attrib.get('bksetname') == "Default Backup Set":          elem.remove(elem.tag)    

Output:

  Entering elem:  self._children.remove(element)  child2  ValueError: list.remove(x): x not in list    

I am trying to delete the element by searching the element in file, if it has the attribute I want.I tried this way in my code and getting error in my output as shared. Please tell me how can I do this using xml.etree.ElementTree package.

No comments:

Post a Comment