title: how to remove specifc tags in xml using python
body: <p>I have to remove some specific tag in apache-tomcat web.xml files </p>
  
  <h1>web.xml</h1>
  
  <pre><code>    &lt;?xml version="1.0" encoding="ISO-8859-1"?&gt;
  
  
  
  &lt;web-app xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                        http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0"&gt;
  
    &lt;!-- ======================== Introduction ============================== --&gt;
    &lt;!-- This document defines default values for *all* web applications      --&gt;
    &lt;!-- loaded into this instance of Tomcat.  As each application is         --&gt;
    &lt;!-- deployed, this file is processed, followed by the                    --&gt;
    &lt;!-- "/WEB-INF/web.xml" deployment descriptor from your own               --&gt;
    &lt;!-- applications.                                                        --&gt;
    &lt;!--                                                                      --&gt;
    &lt;!-- WARNING:  Do not configure application-specific resources here!      --&gt;
    &lt;!-- They should go in the "/WEB-INF/web.xml" file in your application.   --&gt;
  
       &lt;servlet&gt;
          &lt;servlet-name&gt;default&lt;/servlet-name&gt;
          &lt;servlet-class&gt;org.apache.catalina.servlets.DefaultServlet&lt;/servlet-class&gt;
          &lt;init-param&gt;
              &lt;param-name&gt;debug&lt;/param-name&gt;
              &lt;param-value&gt;0&lt;/param-value&gt;
          &lt;/init-param&gt;
          &lt;init-param&gt;
              &lt;param-name&gt;listings&lt;/param-name&gt;
              &lt;param-value&gt;false&lt;/param-value&gt;
          &lt;/init-param&gt;
          &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
      &lt;/servlet&gt;
     &lt;servlet&gt;
          &lt;servlet-name&gt;jsp&lt;/servlet-name&gt;
          &lt;servlet-class&gt;org.apache.jasper.servlet.JspServlet&lt;/servlet-class&gt;
          &lt;init-param&gt;
              &lt;param-name&gt;fork&lt;/param-name&gt;
              &lt;param-value&gt;false&lt;/param-value&gt;
          &lt;/init-param&gt;
          &lt;init-param&gt;
              &lt;param-name&gt;xpoweredBy&lt;/param-name&gt;
              &lt;param-value&gt;false&lt;/param-value&gt;
          &lt;/init-param&gt;
          &lt;load-on-startup&gt;3&lt;/load-on-startup&gt;
      &lt;/servlet&gt;
  
      &lt;servlet&gt;
          &lt;servlet-name&gt;cgi&lt;/servlet-name&gt;
          &lt;servlet-class&gt;org.apache.catalina.servlets.CGIServlet&lt;/servlet-class&gt;
          &lt;init-param&gt;
            &lt;param-name&gt;debug&lt;/param-name&gt;
            &lt;param-value&gt;0&lt;/param-value&gt;
          &lt;/init-param&gt;
          &lt;init-param&gt;
            &lt;param-name&gt;cgiPathPrefix&lt;/param-name&gt;
            &lt;param-value&gt;WEB-INF/cgi&lt;/param-value&gt;
          &lt;/init-param&gt;
           &lt;load-on-startup&gt;5&lt;/load-on-startup&gt;
      &lt;/servlet&gt;
  &lt;/&lt;web-app&gt;
  </code></pre>
  
  <p>if servlet-name== cgi i need to remove entier servlet tag. 
      my code as follows:</p>
  
  <pre><code>    from xml.etree.ElementTree import ElementTree
      tree = ElementTree()
      tree.parse('web.xml')
      servlets = tree.findall('servlet')
      print "servlets : ",servlets
      for servlet in servlets:
        servlet_names = foo.findall('servlet-name')
        for servlet_name  in servlet_names:
              if servlet_name == "cgi" :
                      print "servlet_name :", servlet_name
                      servlet.remove(servlet-name)
  </code></pre>
  
  <p>I am getting o/p as servlets :  []
          instead of all servlets and unable to enter the for loop. 
          Can any one help me ?.</p>
  
  <h1>I am not getting Any exception</h1>
  
  <pre><code>#!/usr/bin/python
  from xml.etree.ElementTree import ElementTree
  tree = ElementTree()
  tree.parse('web.xml')
  root = tree.getroot()
  ns = '{http://java.sun.com/xml/ns/javaee}'
  servlets = root.findall(ns + 'servlet')
  print "servlets : ",servlets
  for servlet in servlets:
    servlet_names = servlet.findall(ns + 'servlet-name')
    for servlet_name  in servlet_names:
          if servlet_name.text == "cgi" :
                  print "servlet_name :", servlet_name.text
                  print "removed the cgi serverlet", root.remove(servlet)
  </code></pre>
  
  <p>=====output=============== 
  servlets :  [http://java.sun.com/xml/ns/javaee}servlet at 7f84e09b35a8>, http://java.sun.com/xml/ns/javaee}servlet at 7f84e09b3878>, http://java.sun.com/xml/ns/javaee}servlet at 7f84e09b3bd8>]
  servlet_name : cgi
  removed the cgi serverlet None</p>
  
  <p>====  i have used pdb tracer to find out the element(servlet) value its shwoing as \n.. </p>
  
  <pre><code>&gt; /apps/manu/python/manunamespace.py(10)&lt;module&gt;()
  -&gt; servlet_name=servlet.find('{http://java.sun.com/xml/ns/javaee}servlet-name')
  (Pdb) servlet_name
  &lt;Element {http://java.sun.com/xml/ns/javaee}servlet-name at 882878&gt;
  (Pdb) servlet_name.text
  'jsp'
  (Pdb) n
  &gt; /apps/manu/python/manunamespace.py(11)&lt;module&gt;()
  -&gt; print "servlet_name:", servlet_name.text
  (Pdb) servlet_name.text
  'cgi'
  (Pdb) servlet.text
  '\n        '
  (Pdb) n
  servlet_name: cgi
  &gt; /apps/manu/python/manunamespace.py(12)&lt;module&gt;()
  -&gt; if servlet_name.text == "cgi":
  (Pdb) n
  &gt; /apps/manu/python/manunamespace.py(13)&lt;module&gt;()
  -&gt; print "remove the element"
  (Pdb) n
  remove the element
  &gt; /apps/manu/python/manunamespace.py(14)&lt;module&gt;()
  -&gt; print "remove : ",root.remove(servlet)
  (Pdb) servlet
  &lt;Element {http://java.sun.com/xml/ns/javaee}servlet at 882d88&gt;
  (Pdb) servlet.text
  '\n 
  
     '
  </code></pre>