title: Adding child element to XML in python
body: <p>I have an element in an XML file as: </p>
  
  <pre><code>&lt;condition&gt;
    &lt;comparison compare="and"&gt;
      &lt;operand idref="XXX" type="boolean" /&gt;
    &lt;/comparison&gt;
  &lt;/condition&gt;
  </code></pre>
  
  <p>I need to add two other child elements (child1 and child2) such as:</p>
  
  <pre><code>&lt;condition&gt;
    &lt;child1 compare='and'&gt;
      &lt;child2 idref='False' type='int' /&gt; 
      &lt;comparison compare="and"&gt;
        &lt;operand idref="XXX" type="boolean" /&gt;
      &lt;/comparison&gt;
    &lt;/child1&gt;
  &lt;/condition&gt;
  </code></pre>
  
  <p>I proceeded using lxml:</p>
  
  <pre><code>from lxml import etree
  tree = etree.parse(xml_file)
  condition_elem = tree.find("&lt;path for the condition block in the xml&gt;")
  etree.SubElement(condition_elem, 'child1')
  tree.write( 'newXML.xml', encoding='utf-8', xml_declaration=True)
  </code></pre>
  
  <p>This just adds the element child1 as a child  of the element condition as below and did not satisfy my requirement:</p>
  
  <pre><code>&lt;condition&gt;
    &lt;child1&gt;&lt;/child1&gt;
    &lt;comparison compare="and"&gt;
      &lt;operand idref="XXX" type="boolean" /&gt;
    &lt;/comparison&gt;
  &lt;/condition&gt;
  </code></pre>
  
  <p>Any ideas? Thanks</p>