title: merging xml files using python's ElementTree
body: <p>I need to merge two xml files on the third block of the xml. So, files A.xml and B.xml look like this:</p>
  
  <p>A.xml</p>
  
  <pre><code>&lt;sample id="1"&gt;
  &lt;workflow value="x" version="1"/&gt;
    &lt;results&gt;
     &lt;result type="T"&gt;
        &lt;result_data type="value" value="19"/&gt;
        &lt;result_data type="value" value="15"/&gt;
        &lt;result_data type="value" value="14"/&gt;
        &lt;result_data type="value" value="13"/&gt;
        &lt;result_data type="value" value="12"/&gt;
      &lt;/result&gt;
    &lt;/results&gt;
  &lt;/sample&gt;
  </code></pre>
  
  <p>B.xml</p>
  
  <pre><code>&lt;sample id="1"&gt;
  &lt;workflow value="x" version="1"/&gt;
    &lt;results&gt;
     &lt;result type="Q"&gt;
        &lt;result_data type="value" value="11"/&gt;
        &lt;result_data type="value" value="21"/&gt;
        &lt;result_data type="value" value="13"/&gt;
        &lt;result_data type="value" value="12"/&gt;
        &lt;result_data type="value" value="15"/&gt;
      &lt;/result&gt;
    &lt;/results&gt;
  &lt;/sample&gt;
  </code></pre>
  
  <p>I need to merge on 'results'</p>
  
  <pre><code>&lt;sample id="1"&gt;
  &lt;workflow value="x" version="1"/&gt;
    &lt;results&gt;
     &lt;result type="T"&gt;
        &lt;result_data type="value" value="19"/&gt;
        &lt;result_data type="value" value="15"/&gt;
        &lt;result_data type="value" value="14"/&gt;
        &lt;result_data type="value" value="13"/&gt;
        &lt;result_data type="value" value="12"/&gt;
     &lt;/result&gt;
     &lt;result type="Q"&gt;
        &lt;result_data type="value" value="11"/&gt;
        &lt;result_data type="value" value="21"/&gt;
        &lt;result_data type="value" value="13"/&gt;
        &lt;result_data type="value" value="12"/&gt;
        &lt;result_data type="value" value="15"/&gt;
     &lt;/result&gt;
    &lt;/results&gt;
  &lt;/sample&gt;
  </code></pre>
  
  <p>What I have done so far is this:</p>
  
  <pre><code>import os, os.path, sys
  import glob
  from xml.etree import ElementTree
  
  def run(files):
      xml_files = glob.glob(files +"/*.xml")
      xml_element_tree = None
      for xml_file in xml_files:
          # get root
          data = ElementTree.parse(xml_file).getroot()
          # print ElementTree.tostring(data)
          for result in data.iter('result'):
              if xml_element_tree is None:
                  xml_element_tree = data 
              else:
                  xml_element_tree.extend(result) 
      if xml_element_tree is not None:
          print ElementTree.tostring(xml_element_tree)
  </code></pre>
  
  <p>As you can see, I assign the initial xml_element_tree to data which has the heading etc, and then extend with 'result'.  However, this gives me this:</p>
  
  <pre><code>&lt;sample id="1"&gt;
  &lt;workflow value="x" version="1"/&gt;
    &lt;results&gt;
     &lt;result type="T"&gt;
        &lt;result_data type="value" value="19"/&gt;
        &lt;result_data type="value" value="15"/&gt;
        &lt;result_data type="value" value="14"/&gt;
        &lt;result_data type="value" value="13"/&gt;
        &lt;result_data type="value" value="12"/&gt;
     &lt;/result&gt;
    &lt;/results&gt;
     &lt;result_data type="value" value="11"/&gt;
        &lt;result_data type="value" value="21"/&gt;
        &lt;result_data type="value" value="13"/&gt;
        &lt;result_data type="value" value="12"/&gt;
        &lt;result_data type="value" value="15"/&gt;
     &lt;/result&gt;
  &lt;/sample&gt;
  </code></pre>
  
  <p>where the results need to be at the bottom.  Any help will be appreciated.</p>