Python XML add element to file
<p>I have about 1000 xml files, and some are missing an element.  I made a script to search through the xml's and print the element.  I wanted to add the ability to add the element if it isnt there, but am unsuccessful.</p>

<p>As you can see below, the DVT3 isnt there and it needs to be added.</p>

<p>My Code</p>

<pre><code>XMLParser = etree.XMLParser(remove_blank_text=True)
for f in os.listdir(directory):
    if f.endswith(".xml"):

        xmlfile = directory + '/' + f


        tree = etree.parse(xmlfile, parser=XMLParser)
        root = tree.getroot()

        hardwareRevisionNode = root.find(".//hardwareRevision")

        try:
            print f + ' :   ' + hardwareRevisionNode.text
        except Exception as e:
            print str(e)
            print xmlfile
            #Wearable = root.find(".//Wearable")
            ChildNode = etree.Element(".//Wearable")
            ChildNode.text = "DVT2"
            ChildNode.append(ChildNode)
            tree.write(xmlfile, pretty_print=True)
</code></pre>

<p>XML File</p>

<pre><code>&lt;?xml version='1.0' encoding='UTF-8' standalone='yes' ?&gt;
&lt;Speech&gt;
  &lt;Results&gt;
    &lt;breakpoint name="ASR_START_RECOGNITION" elapsedTime="00:00:00.000" /&gt;
  &lt;/Results&gt;
  &lt;Meta&gt;
    &lt;Dialog&gt;
      &lt;sessionUUID&gt;7c9b1e3a-b22f-4793-818f-72bc6e7b84a9&lt;/sessionUUID&gt;
    &lt;/Dialog&gt;
    &lt;ASR&gt;
      &lt;engine&gt;
        &lt;name&gt;Rockhopper&lt;/name&gt;
        &lt;version&gt;1.0.0.61&lt;/version&gt;
      &lt;/engine&gt;
      &lt;wrapper&gt;
        &lt;name&gt;RockhopperAsrEngine&lt;/name&gt;
        &lt;version&gt;1.8.2&lt;/version&gt;
      &lt;/wrapper&gt;
      &lt;wrapper&gt;
        &lt;name&gt;Core&lt;/name&gt;
        &lt;version&gt;1.8.2&lt;/version&gt;
      &lt;/wrapper&gt;
      &lt;resource&gt;
        &lt;name&gt;Language Model&lt;/name&gt;
        &lt;version&gt;1.4.4&lt;/version&gt;
      &lt;/resource&gt;
    &lt;/ASR&gt;
    &lt;Application&gt;
      &lt;name&gt;FightClub&lt;/name&gt;
      &lt;version&gt;0.1.550&lt;/version&gt;
      &lt;commit&gt;8f7a411&lt;/commit&gt;
      &lt;buildDate&gt;2016-03-09T18:16Z&lt;/buildDate&gt;
      &lt;branch&gt;HEAD&lt;/branch&gt;
    &lt;/Application&gt;
    &lt;Wearable&gt;
      &lt;firmware&gt;1.0.183 - FCB1APP000-1611W0183&lt;/firmware&gt;
    &lt;/Wearable&gt;
  &lt;/Meta&gt;
&lt;/Speech&gt;
</code></pre>

<p>XML file I Want</p>

<pre><code>&lt;?xml version='1.0' encoding='UTF-8' standalone='yes' ?&gt;
&lt;Speech&gt;
  &lt;Results&gt;
    &lt;breakpoint name="ASR_START_RECOGNITION" elapsedTime="00:00:00.000" /&gt;
  &lt;/Results&gt;
  &lt;Meta&gt;
    &lt;Dialog&gt;
      &lt;sessionUUID&gt;7c9b1e3a-b22f-4793-818f-72bc6e7b84a9&lt;/sessionUUID&gt;
    &lt;/Dialog&gt;
    &lt;ASR&gt;
      &lt;engine&gt;
        &lt;name&gt;Rockhopper&lt;/name&gt;
        &lt;version&gt;1.0.0.61&lt;/version&gt;
      &lt;/engine&gt;
      &lt;wrapper&gt;
        &lt;name&gt;RockhopperAsrEngine&lt;/name&gt;
        &lt;version&gt;1.8.2&lt;/version&gt;
      &lt;/wrapper&gt;
      &lt;wrapper&gt;
        &lt;name&gt;Core&lt;/name&gt;
        &lt;version&gt;1.8.2&lt;/version&gt;
      &lt;/wrapper&gt;
      &lt;resource&gt;
        &lt;name&gt;Language Model&lt;/name&gt;
        &lt;version&gt;1.4.4&lt;/version&gt;
      &lt;/resource&gt;
    &lt;/ASR&gt;
    &lt;Application&gt;
      &lt;name&gt;FightClub&lt;/name&gt;
      &lt;version&gt;0.1.550&lt;/version&gt;
      &lt;commit&gt;8f7a411&lt;/commit&gt;
      &lt;buildDate&gt;2016-03-09T18:16Z&lt;/buildDate&gt;
      &lt;branch&gt;HEAD&lt;/branch&gt;
    &lt;/Application&gt;
    &lt;Wearable&gt;
      &lt;firmware&gt;1.0.183 - FCB1APP000-1611W0183&lt;/firmware&gt;
      &lt;hardwareRevision&gt;DVT3&lt;/hardwareRevision&gt;
    &lt;/Wearable&gt;
  &lt;/Meta&gt;
&lt;/Speech&gt;
</code></pre>