2

Here's the structure:

   <foo>
        <bar> 
    <buildCommand>
        <name>com.android.ide.eclipse.adt.ApkBuilder</name>
        <arguments>
        </arguments>
    </buildCommand>
    <buildCommand>
        <name>org.eclipse.ui.externaltools.ExternalToolBuilder</name>
        <triggers>auto,full,incremental,</triggers>
    </buildCommand>
        </bar>
   </foo>

and here's my logic, which identifies the buildCommand I want to delete (the second one), adds it to a list, and then does a remove.

import os;
import xml.etree.ElementTree as ET

document = ET.parse("foo"); 
root = document.getroot(); 
removeList = list()
for child in root.iter('buildCommand'): 
   if (child.tag == 'buildCommand'): 
      name = child.find('name').text
      if (name == 'org.eclipse.ui.externaltools.ExternalToolBuilder'):
          removeList.append(child)

for tag in removeList:
   root.remove(tag)

document.write("newfoo")

Python 2.7.1 has the remove command but I get an error on remove:

File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/etree/ElementTree.py", line 337, in remove self._children.remove(element) ValueError: list.remove(x): x not in list

UPDATE:

* Solved by @martijn-pieters - correct logic for second for loop is

for tag in removeList:
   parent = root.find('bar')
   parent.remove(tag)
Scott C Wilson
  • 19,102
  • 10
  • 61
  • 83

1 Answers1

3

You need to remove the element from its parent; you'll need to get a reference to the parent directly though, there is no path from child back up. In this case you'll have to get a reference to the <bar> element at the same time as finding the <buildCommand> element.

Trying to remove the tag from the root fails because the tag is not a direct child of the root.

Scott C Wilson
  • 19,102
  • 10
  • 61
  • 83
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Thanks! Updated the question to show your answer. Is this the most pythonic way to do this kind of "XML editing" or should I use another approach, like building a parallel document and just excluding that node? – Scott C Wilson Mar 23 '13 at 11:18
  • 1
    I'd switch to `lxml` instead; it supports the same API but adds better XPath support plus access to the parent of a node. – Martijn Pieters Mar 23 '13 at 11:20
  • I have not gone the lxml route for now because I'm on Mac OSX 10.7, and it doesn't have lxml - here are ideas for folks who want lxml on that platform though: http://stackoverflow.com/questions/7961577/need-help-installing-lxml-on-os-x-10-7 – Scott C Wilson Mar 23 '13 at 11:24
  • I'm on Mac and I use `lxml` all the time. :-) – Martijn Pieters Mar 23 '13 at 11:29