title: How to deepcopy a xml sub element in Python
body: <p>I am very new to XML and Python and putting things together from posts in this site and others. I am trying to deepcopy an element and append to another element using ElementTree. Starting with the input table below:  </p>
  
  <pre><code>&lt;Company&gt;
      &lt;Identifier&gt;Benesolv&lt;/Identifier&gt;
      &lt;Employee&gt;
          &lt;EESSN&gt;111111111&lt;/EESSN&gt;
          &lt;Dependent&gt;
              &lt;SSN&gt;222222222&lt;/SSN&gt;
          &lt;/Dependent&gt;
      &lt;/Employee&gt;
  &lt;/Company&gt;
  </code></pre>
  
  <p>This is the code I have that works, but not how I want it to ultimately.</p>
  
  <pre><code>import xml.etree.ElementTree as ET
  import copy
  
  tree = ET.parse("file.xml")
  root = tree.getroot()
  
  x = copy.deepcopy(root[0])
  for elem in tree.findall("Company/Employee/Dependent"):
      elem.append(copy.deepcopy(x))
  
  tree.write("file.xml")
  </code></pre>
  
  <p>Here is the output which copies the <code>&lt;Identifier&gt;</code> element and appends to the <code>&lt;Dependent&gt;</code> element:</p>
  
  <pre><code>&lt;Company&gt;
      &lt;Identifier&gt;Benesolv&lt;/Identifier&gt;
      &lt;Employee&gt;
          &lt;EESSN&gt;111111111&lt;/EESSN&gt;
          &lt;Dependent&gt;
              &lt;SSN&gt;222222222&lt;/SSN&gt;
          &lt;Identifier&gt;Benesolv&lt;/Identifier&gt;
      &lt;/Dependent&gt;
      &lt;/Employee&gt;
  &lt;/Company&gt;
  </code></pre>
  
  <p>What I want to do is copy the  <code>&lt;EESSN&gt;</code>  element and append to the <code>&lt;Dependent&gt;</code> element, but I do not know how. I figure that I just replace the <code>root[0]</code> with something that will identify the <code>&lt;EESSN&gt;</code> element, but everything I've tried isn't working. Thanks for the help.</p>
  
  <p>Desired output:</p>
  
  <pre><code>&lt;Company&gt;
      &lt;Identifier&gt;Benesolv&lt;/Identifier&gt;
      &lt;Employee&gt;
          &lt;EESSN&gt;111111111&lt;/EESSN&gt;
          &lt;Dependent&gt;
              &lt;SSN&gt;222222222&lt;/SSN&gt;
          &lt;EESSN&gt;111111111&lt;/EESSN&gt;
          &lt;/Dependent&gt;
      &lt;/Employee&gt;
  &lt;/Company&gt;
  </code></pre>