Counting Elements in an xml file

O

Ouray Viney

Hi All:

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".

I have review the python docs and various python ebooks.

Does anyone have any experience with this sort of thing? If so, could
you suggest a good library and possibly some samples?

Thanks
 
M

Marco Bizzarri

Hi All:

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".

I have review the python docs and various python ebooks.

Does anyone have any experience with this sort of thing? If so, could
you suggest a good library and possibly some samples?

Isn't the SAX part of this howto

http://pyxml.sourceforge.net/topics/howto/xml-howto.html

enough for you to create your parser?


Regards
Marco
 
F

Fredrik Lundh

Ouray said:
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>
 
P

Paul Boddie

<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".

With XPath-capable libraries, it should be enough to execute an XPath
query on the document. For example:

import libxml2dom
d = libxml2dom.parse(filename)
number_of_cases = d.xpath("count(//Testcase[@execute='true'])")

This applies the XPath count function to all Testcase elements in the
document having an execute attribute with a value of 'true', thus
returning the number of matching elements.

Paul
 
O

Ouray Viney

<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".

With XPath-capable libraries, it should be enough to execute an XPath
query on the document. For example:

  import libxml2dom
  d = libxml2dom.parse(filename)
  number_of_cases = d.xpath("count(//Testcase[@execute='true'])")

This applies the XPath count function to all Testcase elements in the
document having an execute attribute with a value of 'true', thus
returning the number of matching elements.

Paul

Hi All:

Thank you very much for all your valuable input. All the examples
provided are exactly what I need to get started.

Enjoy the long weekend (for those in North America).

Cheers
 
G

Gerard flanagan

Ouray said:
Hi All:

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".

You might try the `count` function in the module here:

http://gflanagan.net/python/projects/python-rattlebag/elementfilter.html

The pseudo xpath would be something like:

/Testcase[@execute=="true"]

hth

G.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top