title: Extract part of xml file with python etree
body: <p>I have big xml file that looks as one below. I have put just part of it, as it is >2gb, so just that you see the structure. Basicly all <code>SubNetwork parents</code> have same structure as the one I showed below. What I want to do is to extract only part of this xml file with the <code>&lt;ManagedElementId string="xxxx" /&gt;</code> (where xxx i the input variable). Here is my code and xml:</p>
  
  <pre><code>&lt;Create&gt; 
  &lt;SubNetwork networkType="GSM" userLabel="BSC"&gt;
  .
  .
  &lt;/SubNetwork&gt;
  &lt;SubNetwork networkType="WCDMA" userLabel="RNC01"&gt;
  .
  .
  &lt;/SubNetwork&gt;
  &lt;SubNetwork networkType="IPRAN" userLabel="IPRAN"&gt;
  .
  .
  &lt;/SubNetwork&gt;
  &lt;SubNetwork networkType="WCDMA" userLabel="RNC02"&gt;
                    &lt;ManagedElement sourceType="CELLO"&gt;
                       &lt;ManagedElementId string="3GALPAS" /&gt;
                       &lt;primaryType type="RBS" /&gt;
                     .
                     .
                    &lt;/ManagedElement&gt;
                    &lt;ManagedElement sourceType="CELLO"&gt;
                       &lt;ManagedElementId string="3GTUTI" /&gt;
                       &lt;primaryType type="RBS" /&gt;
                     .
                     .
                    &lt;/ManagedElement&gt;
                      &lt;ManagedElement sourceType="CELLO"&gt;
                       &lt;ManagedElementId string="3GHHH" /&gt;
                       &lt;primaryType type="RBS" /&gt;
                     .
                     .
                    &lt;/ManagedElement&gt;
  &lt;/SubNetwork&gt;
  &lt;/Create&gt; 
  </code></pre>
  
  <p>and the code</p>
  
  <pre><code>from xml.etree import ElementTree
  import xml.etree.ElementTree as ET
  from xml.etree.ElementTree import XML, fromstring, tostring
  from xml.etree.ElementTree import Element
  from xml.etree.ElementTree import SubElement
  from xml.etree.ElementTree import Element, SubElement, Comment
  
  
  with open(r"C:\\Users\\etihkru\\Desktop\\h4.xml", 'rt') as f:
     root = ET.parse(f)
     tree=root.getroot()
     with open(r"C:\\Users\\etihkru\\Desktop\\list_of_xxx", 'r') as f2:
          for line in f2:
               line=line.rstrip()
               line1='"' + line + '"'
               xp_str1 = str(('.//ManagedElementId[@string='))
               xp_str2 = str("]/../../")
               str_elem = xp_str1 + line1 + xp_str2 
               for item in tree.findall(str_elem):
                      print ET.tostring(item)
  </code></pre>
  
  <p>and file <code>list_of_xxx</code> is as below:</p>
  
  <pre><code>3GALPAS
  3GTUTI
  </code></pre>
  
  <p>As said there is numerues number of  <code>&lt;ManagedElementId string=/&gt;</code>, and I just want to extract the ones that are in <code>list_of_xxx</code>.</p>
  
  <p>So I want output as below:</p>
  
  <pre><code>&lt;SubNetwork networkType="WCDMA" userLabel="RNC02"&gt;
                    &lt;ManagedElement sourceType="CELLO"&gt;
                       &lt;ManagedElementId string="3GALPAS" /&gt;
                       &lt;primaryType type="RBS" /&gt;
                     .
                     .
                    &lt;/ManagedElement&gt;
  &lt;/SubNetwork&gt;
  &lt;SubNetwork networkType="WCDMA" userLabel="RNC02"&gt;
                    &lt;ManagedElement sourceType="CELLO"&gt;
                       &lt;ManagedElementId string="3GTUTI" /&gt;
                       &lt;primaryType type="RBS" /&gt;
                     .
                     .
                    &lt;/ManagedElement&gt;
  &lt;/SubNetwork&gt;
  </code></pre>
  
  <p>So, I want to find all <code>ManagedElementIds</code> as given in list_of_xxx,and their parents <code>ManagedElement</code> and <code>SubNetwork</code>, and write them as given above. Every <code>MangedElementid</code> should be closed with parents as mentioned. I'm uing python 2.6 without lxml, as I don't have right to install it.</p>