473,387 Members | 1,603 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Counting Elements in an xml file

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
Aug 30 '08 #1
5 12658
On Sat, Aug 30, 2008 at 7:37 PM, Ouray Viney <ov****@gmail.comwrote:
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/
Aug 30 '08 #2
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 <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>

Aug 30 '08 #3
On 30 Aug, 19:37, Ouray Viney <ovi...@gmail.comwrote:
>
<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
Aug 30 '08 #4
On Aug 30, 2:17*pm, Paul Boddie <p...@boddie.org.ukwrote:
On 30 Aug, 19:37, Ouray Viney <ovi...@gmail.comwrote:
<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
Aug 30 '08 #5
Ouray Viney wrote:
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.

Aug 31 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: John Reese | last post by:
def uselessHash(filename): fp= open(filename) hash= 0 for line in fp: hash ^= hash(line.strip()) fp.close() # do I need this or is fp closed by ref count? return hash Consider the function...
3
by: Michael H Lees | last post by:
Hi all, As I see it there are two options for checking if for an element with key 'x' exists in a map m.... 1.) if(m.find(x)!=m.end()) or 2.) if(m.count(x))
4
by: Victor Engmark | last post by:
When looking for a method to fetch unique elements and counting the number of occurences of each of them, I found quite a lot of gross examples of complex XSL. But after realizing the subtle...
13
by: Don Kim | last post by:
I'm writing a program to read 2,000,000 floating point numbers off a text file, to compute the sum, mean, and median. This is a direct example of Stroustrup's paper. But the program will not...
9
by: Rafal Konopka | last post by:
How can you caount the number of occurrences of elements in an array? For example var arr1 = ; how can I count how many 1s, 2s, 3s, etc.? Despite the example, I never actually know what...
4
by: aaronfude | last post by:
Hi, Please consider the following class (it's not really my class, but it's a good example for my question): class Vector { int myN; double *myX; Vector(int n) : myN(n), myX(new double) { }...
13
by: hawkesed | last post by:
If I have a list, say of names. And I want to count all the people named, say, Susie, but I don't care exactly how they spell it (ie, Susy, Susi, Susie all work.) how would I do this? Set up a...
7
by: peraklo | last post by:
Hello, there is another problem i am facing. i have a text file which is about 15000 lines big. i have to cut the last 27 lines from that file and create a new text file that contans those 27...
16
by: sophia.agnes | last post by:
Hi, is there any guarantee that elements of an un initialized array by default contains the element 0 ?
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.