Connecting Tech Pros Worldwide Forums | Help | Site Map

Counting Elements in an xml file

Ouray Viney
Guest
 
Posts: n/a
#1: Aug 30 '08
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 <TestCaseelements 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

Marco Bizzarri
Guest
 
Posts: n/a
#2: Aug 30 '08

re: Counting Elements in an xml file


On Sat, Aug 30, 2008 at 7:37 PM, Ouray Viney <oviney@gmail.comwrote:
Quote:
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 <TestCaseelements 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/...xml-howto.html

enough for you to create your parser?


Regards
Marco

--
Marco Bizzarri
http://iliveinpisa.blogspot.com/
http://notenotturne.blogspot.com/
Fredrik Lundh
Guest
 
Posts: n/a
#3: Aug 30 '08

re: Counting Elements in an xml file


Ouray Viney wrote:
Quote:
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 <TestCaseelements 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>

Paul Boddie
Guest
 
Posts: n/a
#4: Aug 30 '08

re: Counting Elements in an xml file


On 30 Aug, 19:37, Ouray Viney <ovi...@gmail.comwrote:
Quote:
>
<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
Ouray Viney
Guest
 
Posts: n/a
#5: Aug 30 '08

re: Counting Elements in an xml file


On Aug 30, 2:17*pm, Paul Boddie <p...@boddie.org.ukwrote:
Quote:
On 30 Aug, 19:37, Ouray Viney <ovi...@gmail.comwrote:
>
>
>
Quote:
<Testcase execute="true" name="foobar">
>
Quote:
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
Gerard flanagan
Guest
 
Posts: n/a
#6: Aug 31 '08

re: Counting Elements in an xml file


Ouray Viney wrote:
Quote:
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 <TestCaseelements 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...entfilter.html

The pseudo xpath would be something like:

/Testcase[@execute=="true"]

hth

G.

Closed Thread