Counting Elements in an xml file

Fredrik Lundh fredrik at pythonware.com
Sat Aug 30 13:48:43 EDT 2008


Ouray Viney wrote:

> I am looking at writing a python script that will let me parse a
> TestSuite xml file that contains n number of TestCases.
> 
> My goal is to be able to count the <TestCase> elements base on a key
> value pair in the xml node.
> 
> Example
> 
> <Testcase execute="true" name="foobar">
> 
> I would like to be able to count the number of TestCases that contain
> the "execute=true" but not the ones that contain "execute=false".

import xml.etree.ElementTree as ET

tree = ET.parse("filename.xml")

count = 0

for elem in tree.findall(".//Testcase"):
     if elem.get("execute") == "true":
         count += 1

print "found", count, "test cases"

# tweak as necessary

</F>




More information about the Python-list mailing list