title: XML reading the last entry from the file in python
body: <p>I want to read the last entry of the xml file and get its value. Here is <strong>my xml file</strong></p>
  
  <pre><code>&lt;TestSuite&gt;
    &lt;TestCase&gt;
      &lt;name&gt;tcname1&lt;/name&gt;
      &lt;total&gt;1&lt;/total&gt;
      &lt;totalpass&gt;0&lt;/totalpass&gt;
      &lt;totalfail&gt;0&lt;/totalfail&gt;
      &lt;totalerror&gt;1&lt;/totalerror&gt;
    &lt;/TestCase&gt;
    &lt;TestCase&gt;
      &lt;name&gt;tcname2&lt;/name&gt;
      &lt;total&gt;1&lt;/total&gt;
      &lt;totalpass&gt;0&lt;/totalpass&gt;
      &lt;totalfail&gt;0&lt;/totalfail&gt;
      &lt;totalerror&gt;1&lt;/totalerror&gt;
    &lt;/TestCase&gt;
  &lt;/TestSuite&gt;
  </code></pre>
  
  <p>I want to get the <code>&lt;total&gt;</code> , <code>&lt;totalpass&gt;</code>,<code>&lt;totalfail&gt;</code> and <code>&lt;totalerror&gt;</code> value in the last tag of the file. I have tried this code to do that.</p>
  
  <pre><code>import xmltodict
  with open(filename) as fd:
      doc = xmltodict.parse(fd.read())
      length=len(doc['TestSuite']['TestCase'])
      tp=doc['TestSuite']['TestCase'][length-1]['totalpass']
      tf=doc['TestSuite']['TestCase'][length-1]['totalfail']
      te=doc['TestSuite']['TestCase'][length-1]['totalerror']
      total=doc['TestSuite']['TestCase'][length-1]['total']
  </code></pre>
  
  <p>This works for the xml with 2 or more testcase tags in xml files , But fails with this error for the file with only one testcase tag .</p>
  
  <pre><code>Traceback (most recent call last):
    File "HTMLReportGenerationFromXML.py", line 52, in &lt;module&gt;
      tp=doc['TestSuite']['TestCase'][length-1]['totalpass']
  KeyError: 4 .
  </code></pre>
  
  <p>Because instead of the  count , it is taking the subtag (  etc value as length). Please help me resolve this issue.</p>