title: Insert XML Value/Element with Python
body: <p>I've been looking all over for a solution to adding an element/value to a current XML file. So let's assume I have the following XML File:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;
  &lt;Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;
      &lt;Node&gt;
          &lt;Name&gt;Sprinkler&lt;/Name&gt;
          &lt;Type&gt;Blah&lt;/Type&gt;
          &lt;Prob&gt;0.82&lt;/Prob&gt;
      &lt;/Node&gt;
      &lt;Node&gt;
          &lt;Name&gt;Rain&lt;/Name&gt;
          &lt;Type&gt;Bleh&lt;/Type&gt;
          &lt;Prob&gt;0.23&lt;/Prob&gt;
      &lt;/Node&gt;
      &lt;Node&gt;
          &lt;Name&gt;Cloudy&lt;/Name&gt;
          &lt;Type&gt;Bluh&lt;/Type&gt;
          &lt;Prob&gt;0.71&lt;/Prob&gt;
      &lt;/Node&gt;
  &lt;/Root&gt;
  </code></pre>
  
  <p>Now, my goal is, given a CSV file, I want to add new elements and values per Node. Let's say that my CSV contains something like this:</p>
  
  <pre><code>Cloudy,Or,Sprinkler,Rain
  Sprinkler,And,Rain
  Rain,Or,Sprinkler,Cloudy
  </code></pre>
  
  <p>I was able to read the CSV without a problem, my problem is adding new elements, 'Parent0' and 'Parent1'(If any), so that the output looks like this:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;
  &lt;Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;
      &lt;Node&gt;
          &lt;Name&gt;Sprinkler&lt;/Name&gt;
          &lt;Type&gt;Blah&lt;/Type&gt;
          &lt;Prob&gt;0.82&lt;/Prob&gt;
          &lt;Gate&gt;And&lt;/Gate&gt;
          &lt;Parent0&gt;Rain&lt;/Parent0&gt;
      &lt;/Node&gt;
      &lt;Node&gt;
          &lt;Name&gt;Rain&lt;/Name&gt;
          &lt;Type&gt;Bleh&lt;/Type&gt;
          &lt;Prob&gt;0.23&lt;/Prob&gt;
          &lt;Gate&gt;Or&lt;/Gate&gt;
          &lt;Parent0&gt;Sprinkler&lt;/Parent0&gt;
          &lt;Parent1&gt;Cloudy&lt;/Parent1&gt;
      &lt;/Node&gt;
      &lt;Node&gt;
          &lt;Name&gt;Cloudy&lt;/Name&gt;
          &lt;Type&gt;Bluh&lt;/Type&gt;
          &lt;Prob&gt;0.71&lt;/Prob&gt;
          &lt;Gate&gt;Or&lt;/Gate&gt;
          &lt;Parent0&gt;Sprinkler&lt;/Parent0&gt;
          &lt;Parent1&gt;Rain&lt;/Parent1&gt;
      &lt;/Node&gt;
  &lt;/Root&gt;
  </code></pre>
  
  <p>So far I have written the following in Python:</p>
  
  <pre><code>import xml.etree.ElementTree as ET
  
  xml = ET.parse('XML.xml')
  for row in xml.iterfind('Node'):
      i = 1
      for item in csvFile:
          row.append('&lt;Gate&gt;'+item[1]+'&lt;/Gate&gt;\n')
          if i != 1:
              for x in xrange(0, len(item):
                  if row.findtext('Name') == item[x]:
                      row.append('&lt;Parent0&gt;'+item[x]+'&lt;/Parent0&gt;\n')
          else:
              i = 0
  </code></pre>
  
  <p>On my code it will all go to Parent0 for now, but I wanted to see how it is possible to do the append, without deleting everything? I have a heard of lxml and minidom but not sure how these work. If I am able to do it using xml.etree.ElementTree, that would be fantastic.</p>