Computer Science Mojo

~ David's Notes on coding, software and computer science




Post

How to check tag exists when parsing XML in Python

Category: Python     Tag: Coding   Errors   Python   XML  
By: David     On: Tue 28 July 2015     

When using Python minidom to parse XML it is easy to encounter an "IndexError: list index out of range" if the tag does not exit. An easy fix is to check the size of "doc.getElementsByTagName".
Alt Text

Software

  • Python 2.7

Reading

Parse the XML file "websites.xml" :

<?xml version="1.0"?>
<website>
  <title>csmojo</title>
  <description>computer science mojo</description>
</website>
  • Python parsing code without size check:
from xml.dom import minidom
doc = minidom.parse("websites.xml")

title = doc.getElementsByTagName("title")[0]
titleData = title.firstChild.data
print titleData

idTag = doc.getElementsByTagName("id")[0]
idData = idTag.firstChild.data
print idData

this prints:
Alt Text

  • Python parsing code with size check:
from xml.dom import minidom
doc = minidom.parse("websites.xml")
titleData = None
idData = None

allTitle = doc.getElementsByTagName("title")
if (allTitle.length > 0):
    titleData = allTitle[0].firstChild.data
else:
    titleData = "None"
print titleData

allId = doc.getElementsByTagName("id")
if (allId.length > 0):
    idData = allId[0].firstChild.data
else:
    idData = "None"
print idData

this prints:
Alt Text