title: Update element values using xml.dom.minidom
body: <p>I have an XML structure which looks similar to:</p>
  
  <pre><code>&lt;Store&gt;
     &lt;foo&gt;
        &lt;book&gt;
          &lt;isbn&gt;123456&lt;/isbn&gt;
        &lt;/book&gt;
        &lt;title&gt;XYZ&lt;/title&gt;
        &lt;checkout&gt;no&lt;/checkout&gt;
     &lt;/foo&gt;
  
     &lt;bar&gt;
        &lt;book&gt;
          &lt;isbn&gt;7890&lt;/isbn&gt;
        &lt;/book&gt;
        &lt;title&gt;XYZ2&lt;/title&gt;
        &lt;checkout&gt;yes&lt;/checkout&gt;
     &lt;/bar&gt;
  &lt;/Store&gt;
  </code></pre>
  
  <p>Using xml.dom.minidom only (restrictions) i would like to </p>
  
  <p>1)traverse through the XML file</p>
  
  <p>2)Search/Get for particular element, depending on its parent</p>
  
  <p>Example: checkout element for author1, isbn for author2</p>
  
  <p>3)Change/Set that element's value</p>
  
  <p>4)Write the new XML structure to a file</p>
  
  <p>Can anyone help here?</p>
  
  <p>Thank you!</p>
  
  <p><strong>UPDATE</strong>:</p>
  
  <p>This is what i have done till now</p>
  
  <pre><code>import xml.dom.minidom
  checkout = "yes"
  
  def getLoneChild(node, tagname):
  
    assert ((node is not None) and (tagname is not None))
    elem = node.getElementsByTagName(tagname)
    if ((elem is None) or (len(elem) != 1)):
      return None
    return elem
  
  def getLoneLeaf(node, tagname):
  
    assert ((node is not None) and (tagname is not None))
    elem = node.getElementsByTagName(tagname)
    if ((elem is None) or (len(elem) != 1)):
      return None
    leaf = elem[0].firstChild
    if (leaf is None):
      return None
    return leaf.data
  
  
  def setcheckout(node, tagname):
  
    assert ((node is not None) and (tagname is not None))
    child = getLoneChild(node, 'foo')
    Check = getLoneLeaf(child[0],'checkout')
    Check = tagname
    return Check
  
  doc = xml.dom.minidom.parse('test.xml') 
  root = doc.getElementsByTagName('Store')[0]
  output = setcheckout(root, checkout)
  
  tmp_config = '/tmp/tmp_config.xml' 
  fw = open(tmp_config, 'w')
  fw.write(doc.toxml())
  fw.close()
  </code></pre>