Counting number of xml tags in python using xml.dom.minidom
python, xml, xml-parsing
Solution
Try `len(dom.getElementsByTagName('out'))`
from xml.dom.minidom import parseString
file = open('test.xml','r')
data = file.read()
file.close()
dom = parseString(data)
print len(dom.getElementsByTagName('out'))
gives
3
Problem
My XML file test.xml contains the following tags ``` <?xml version="1.0" encoding="ISO-8859-1"?> <AppName> <author>Subho Halder</author> <description> Description</description> <date>2012-11-06</date> <out>Output 1</out> <out>Output 2</out> <out>Output 3</out> </AppName> ``` I want to count the number of times the `<out>` tag has occured This is my python code so far which I have written: ``` from xml.dom.minidom import parseString file = open('test.xml','r') data = file.read() file.close() dom = parseString(data) if (len(dom.getElementsByTagName('author'))!=0): xmlTag = dom.getElementsByTagName('author')[0].toxml() author = xmlTag.replace('<author>','').replace('</author>','') print author ``` Can someone help me out here?