title: How to copy multiple XML nodes to another file in Python
body: <p>Bare in mind I am very new to Python. I'm trying to copy few XML nodes from sample1.xml to out.xml if it doesn't exist in sample2.xml.</p>
  
  <p><strong>this is how far I got before I'm stuck</strong></p>
  
  <pre><code>import xml.etree.ElementTree as ET
  
  tree = ET.ElementTree(file='sample1.xml')
  addtree = ET.ElementTree(file='sample2.xml')
  
  root = tree.getroot()
  addroot = addtree.getroot()
  
  for adel in addroot.findall('.//cars/car'):
      for el in root.findall('cars/car'):
          with open('out.xml', 'w+') as f:
              f.write("BEFORE\n")    
              f.write(el.tag)
              f.write("\n")
              f.write(adel.tag)
              f.write("\n")
              f.write("\n")
  
              f.write("AFTER\n")
  
              el = adel
  
              f.write(el.tag)
              f.write("\n")
              f.write(adel.tag)
  </code></pre>
  
  <p>I have no idea what I'm missing, but it's only copying the actual "<code>tag</code>" itself.</p>
  
  <p><strong>outputs this:</strong></p>
  
  <pre><code>BEFORE
  car
  car
  
  AFTER
  car
  car
  </code></pre>
  
  <p>So I'm missing the children nodes, and also the <code>&lt;</code>, <code>&gt;</code>, <code>&lt;/</code>, <code>&gt;</code> tags. Expected result is below.</p>
  
  <p><strong>sample1.xml:</strong></p>
  
  <pre><code>&lt;cars&gt;
      &lt;car&gt;
          &lt;use-car&gt;0&lt;/use-car&gt;
          &lt;use-gas&gt;0&lt;/use-gas&gt;
          &lt;car-name /&gt;
          &lt;car-key /&gt;
          &lt;car-location&gt;hawaii&lt;/car-location&gt;
          &lt;car-port&gt;5&lt;/car-port&gt;
      &lt;/car&gt;
  &lt;/cars&gt;
  </code></pre>
  
  <p><strong>sample2.xml:</strong></p>
  
  <pre><code>&lt;cars&gt;
      &lt;old&gt;
          1
      &lt;/old&gt;
      &lt;new&gt;
          8
      &lt;/new&gt;
      &lt;car /&gt;
  &lt;/cars&gt;
  </code></pre>
  
  <p><strong>expected result in out.xml (final product)</strong></p>
  
  <pre><code>&lt;cars&gt;
      &lt;old&gt;
          1
      &lt;/old&gt;
      &lt;new&gt;
          8
      &lt;/old&gt;
      &lt;car&gt;
          &lt;use-car&gt;0&lt;/use-car&gt;
          &lt;use-gas&gt;0&lt;/use-gas&gt;
          &lt;car-name /&gt;
          &lt;car-key /&gt;
          &lt;car-location&gt;hawaii&lt;/car-location&gt;
          &lt;car-port&gt;5&lt;/car-port&gt;
      &lt;/car&gt;
  &lt;/cars&gt;
  </code></pre>
  
  <p>All the other nodes <code>old</code> and <code>new</code> must remain untouched. I'm just trying to replace <code>&lt;car /&gt;</code> with all its children and grandchildren (if existed) nodes.</p>