title: Removing parent element and all subelements from XML
body: <p>Given an XML file with the following structure:</p>
  
  <pre><code>&lt;Root&gt;
      &lt;Stuff&gt;&lt;/Stuff&gt;
      &lt;MoreStuff&gt;&lt;/MoreStuff&gt;
      &lt;Targets&gt;
          &lt;Target&gt;
              &lt;ID&gt;12345&lt;/ID&gt;
              &lt;Type&gt;Ground&lt;/Type&gt;
              &lt;Size&gt;Large&lt;/Size&gt;
          &lt;/Target&gt;
          &lt;Target&gt;
              ...
          &lt;/Target&gt;
      &lt;/Targets&gt;
  &lt;/Root&gt;
  </code></pre>
  
  <p>I'm trying to loop through each child under the <code>&lt;Targets&gt;</code> element, check each <code>&lt;ID&gt;</code> for a specific value, and if the value is found, then I want to delete the entire <code>&lt;Target&gt;</code> entry. I've been using the ElementTree Python library with little success. Here's what I have so far:</p>
  
  <pre><code>import xml.etree.ElementTree as ET
  
  tree = ET.parse('file.xml')
  root = tree.getroot()
  
  iterator = root.getiterator('Target')
  
  for item in iterator:
      old = item.find('ID')
      text = old.text
      if '12345' in text:
          item.remove(old)
  
  tree.write('out.xml')
  </code></pre>
  
  <p>The problem I'm having with this approach is that only the <code>&lt;ID&gt;</code> sub element is removed, however I need the entire <code>&lt;Target&gt;</code> element and all of its child elements removed. Can anyone help! Thanks.</p>