Python XML Parsing issue
<p>I have an XML as attached below and using python minidom to parse the build.xml. I am trying below python code to parse and retrieve the "name" and "value" tag. I am trying to retrieve the values for  "SE_CONFIG","SE_ARCH","PREBUILDID" which have respective value install-csu,macosx,prebuild_7701.</p>

<p>Having following challenges.</p>

<ol>
<li>What is better pythonic way to retrieve respective name and value pair</li>
<li><p>How should I catch the exception if there is no "value"</p>

<pre><code>&lt;?xml version='1.0' encoding='UTF-8'?&gt;
&lt;build&gt;
  &lt;actions&gt;
    &lt;hudson.model.ParametersAction&gt;
      &lt;parameters&gt;
        &lt;hudson.model.StringParameterValue&gt;
          &lt;name&gt;StartFrom&lt;/name&gt;
          &lt;description&gt;&amp;lt;h3&amp;gt;: Trigger downstreamfor this platform&amp;lt;br&amp;gt;&lt;/description&gt;
          &lt;value&gt;Fetch_Source&lt;/value&gt;
        &lt;/hudson.model.StringParameterValue&gt;
        &lt;hudson.model.StringParameterValue&gt;
          &lt;name&gt;SE_CONFIG&lt;/name&gt;
          &lt;description&gt;&lt;/description&gt;
          &lt;value&gt;install-csu&lt;/value&gt;
        &lt;/hudson.model.StringParameterValue&gt;
        &lt;hudson.model.StringParameterValue&gt;
          &lt;name&gt;EMAIL_RCPT&lt;/name&gt;
          &lt;description&gt;Please enter your email address.&lt;/description&gt;
          &lt;value&gt;&lt;/value&gt;
        &lt;/hudson.model.StringParameterValue&gt;
        &lt;hudson.model.StringParameterValue&gt;
          &lt;name&gt;SE_ARCH&lt;/name&gt;
          &lt;description&gt;&lt;/description&gt;
          &lt;value&gt;macosx&lt;/value&gt;
        &lt;/hudson.model.StringParameterValue&gt;
        &lt;hudson.model.StringParameterValue&gt;
          &lt;name&gt;PREBUILDID&lt;/name&gt;
          &lt;description&gt;&lt;/description&gt;
          &lt;value&gt;prebuild_7701&lt;/value&gt;
        &lt;/hudson.model.StringParameterValue&gt;
        &lt;hudson.model.StringParameterValue&gt;
          &lt;name&gt;RE_DESCRIPTION&lt;/name&gt;
          &lt;description&gt;&lt;/description&gt;
          &lt;value&gt;&lt;/value&gt;
        &lt;/hudson.model.StringParameterValue&gt;
        &lt;hudson.model.StringParameterValue&gt;
          &lt;name&gt;BUILD_PRODUCT&lt;/name&gt;
          &lt;description&gt;&lt;/description&gt;
          &lt;value&gt;release&lt;/value&gt;
        &lt;/hudson.model.StringParameterValue&gt;
      &lt;/parameters&gt;
    &lt;/hudson.model.ParametersAction&gt;  
  &lt;/actions&gt;
  &lt;number&gt;8065&lt;/number&gt;
  &lt;result&gt;SUCCESS&lt;/result&gt;
  &lt;duration&gt;3652965&lt;/duration&gt;
  &lt;charset&gt;US-ASCII&lt;/charset&gt;
  &lt;keepLog&gt;false&lt;/keepLog&gt;
  &lt;workspace&gt;/Users/someuser/workspace/build-mac&lt;/workspace&gt;
  &lt;hudsonVersion&gt;3.2.1&lt;/hudsonVersion&gt;
  &lt;scm class="hudson.scm.NullChangeLogParser"/&gt;
  &lt;culprits/&gt;
&lt;/build&gt;
</code></pre></li>
</ol>

<hr>

<pre><code>    import xml.dom.minidom
    DOMTree=xml.dom.minidom.parse("build.xml")
    collection=DOMTree.documentElement
    string_par=collection.getElementsByTagName("hudson.model.StringParameterValue")
    for each_node in string_par:
       print each_node.getElementsByTagName('name')[0].childNodes[0].nodeValue
       print each_node.getElementsByTagName('value')[0].childNodes[0].nodeValue

    StartFrom
    Fetch_Source
    SE_CONFIG
    install-csu
    EMAIL_RCPT
    Traceback (most recent call last):
    File "&lt;stdin&gt;", line 3, in &lt;module
    IndexError: list index out of range
</code></pre>

<p>Since you asked if there is another way, you can try using <strong>xml.etree.ElementTree</strong>. </p>

<p>There is a cool example in the following link, the tags can be defined in the for loop:</p>

<p><a href="http://chimera.labs.oreilly.com/books/1230000000393/ch06.html#_solution_96" rel="nofollow">http://chimera.labs.oreilly.com/books/1230000000393/ch06.html#_solution_96</a></p>

<p>I hope it helps.</p>

python xml minidom