How to check tag exists when parsing XML in Python
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".
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:
- 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: