views:

210

answers:

2

I've got some code which draws data from an xml file but it seems to have randomly starting throwing;

Traceback (most recent call last):
  File "C:\Users\mike\Documents\python\arl xml\turn 24 reader", line 52, in <module>
    unitCount   = getText(evoNode.getElementsByTagName("count")[0].childNodes)
IndexError: list index out of range

it was fine for the first couple of times I ran it then, I dunno if I changed it or not by accident, but now it's throwing the error.

This is an example of the section of the xml it's trying to use;

- <unit>
  <count>1200</count> 
  <type>Zweihander Doppelsoldners</type> 
  <typeid>102</typeid> 
  </unit>

and here's the code that it's complains about;

   for unitNode in node.getElementsByTagName('unit'):
      unitName      = getText(evoNode.getElementsByTagName("type")[0].childNodes)
      unitId     = getText(evoNode.getElementsByTagName("typeid")[0].childNodes)
      unitCount   = getText(evoNode.getElementsByTagName("count")[0].childNodes)

      unitList.append("%s x %s" % (unitName, unitCount))

While I accept that it complains about the count line first because count is the highest of the three on the xml file in the units section I'm still not sure why it's complaining, given that it succesfully runs a very similar set of code from which that was cloned and editted.

Anyone know what I can do or can suggest ways to refine the question?

+1  A: 

A simple idea: check if evoNode.getElementsByTagName("count") returns a non-empty list:

counts = evoNode.getElementsByTagName("count")
if counts:
    unitCount = getText(counts[0].childNodes)

Of course, the check should be applied to all the lists retrieved by your code.

One Other thing, you iterate using unitNode, but inside the loop, you access evoNode, which is probably the same for every iteration.

gimel
:o, woops, I missed that I'd used evoNode on everyone when I copied it across, I guess I was a little slack with my editting, thanks for that :D
Way to go Leachrode :P
Teifion
heh, thanks teif, you didn't notice either :P
+1  A: 

As gimel said you should check getElementsByTagName("count") if it returns non empty list, but back to your problem:

If you said that it was working before then my guess that the problem is with the source where you get the XML.

Maiku Mori