Skip to content Skip to sidebar Skip to footer

How To Parse Xml Using Python

I am trying to parse an xml using python for create a result summary file. Below is my code and a snippet of xml, Like the below i have couple of sections with and <

Solution 1:

I'd not use minidom for this task; the DOM API is very cumbersome, verbose, and not suited for searching and matching.

The Python library also includes the xml.etree.ElementTree API, I'd use that instead:

from xml.etree import ElementTree as ET

tree = ET.parse(r'D:\Squish\squish results\Results-On-2013-01-18_0241 PM.xml')
tests = dict()

# Find all <test> elements with a <verification> child:for test in tree.findall('.//test[verification]'):
    passed = len(test.findall(".//result[@type='PASS']"))
    failed = len(test.findall(".//result[@type='FAIL']"))
    tests[test.attrib['name']] = {'pass': passed, 'fail': failed}

The above piece of code counts the number of passed and failed tests per <test> element and stores them in a dictionary, keyed to the name attribute of the <test> element.

I've tested the above code with Python 3.2 and the full XML document from another question you posted, which results in:

{'tst_Setup_menu_2': {'fail': 0, 'pass': 8}}

Solution 2:

Thanks for the posting. i got it working using minidon. still wish to see how can be solved using xml.etree.ElementTree

import sys
import xml.dom.minidom as XY

file = open("Result_Summary.txt", "w")
#tree = XY.parse('D:\\Squish\\squish results\\Results-On-2013-01-18_0241 PM.xml')
#print (str(sys.argv[1]))
tree = XY.parse(sys.argv[1])

Test_name = tree.getElementsByTagName('test')
count_testname =0

file.write('Test Name \t\t\t No:PASS\t\t\t No:FAIL\t \n\n')
for my_Test_name in Test_name:
    count_testname = count_testname+1
    my_Test_name_final = my_Test_name.getAttribute('name')
    if(count_testname > 1):
        #print(my_Test_name_final)
        file.write(my_Test_name_final)
        file.write('\t\t\t\t')
        my_Test_status = my_Test_name.getElementsByTagName('result')
        passcount = 0
        failcount = 0for my_Test_status_1 in my_Test_status:
            my_Test_status_final = my_Test_status_1.getAttribute('type')
            if(my_Test_status_final == 'PASS'):
               passcount = passcount+1if(my_Test_status_final == 'FAIL'):
               failcount = failcount+1
            #print(str(my_Test_status_final))
        file.write(str(passcount))
        #print(passcount)
        file.write('\t\t\t\t')
        file.write(str(failcount))
       # print(failcount)
        file.write('\n')

#print ('loop count: %d' %count_testname)
#print('PASS count: %s' %passcount)
#print('FAIL count: %s' %failcount)
file.close()

Solution 3:

Although not a standard module but well worth the effort of installing is lxml especially if you want to do fast Xml parsing etc IMHO.

Without a full example of your results I guessed at what they would look like.

from lxml import etree

tree = etree.parse("results.xml")

count_result_type = etree.XPath("count(.//result[@type = $name])")

fortestin tree.xpath("//test"):
    print test.attrib['name']
    print"\t# FAILS ", count_result_type(test, name="FAIL")
    print"\t# PASSES", count_result_type(test, name="PASS")

I generated the following running against my guess of your xml, which should give you an idea of what is happening.

tst_case1
    # FAILS  1.0# PASSES 1.0
tst_case0
    # FAILS  0.0# PASSES 1.0
tst_case2
    # FAILS  0.0# PASSES 1.0
tst_case3
    # FAILS  0.0# PASSES 1.0

What I like about lxml is how expressive it can be, YMMV.

Solution 4:

I see you are using Squish. You should check your squish folder under \examples\regressiontesting. There you can find a file called xml2result2html.py. Here you can find an example of converting squish test results into html.

Post a Comment for "How To Parse Xml Using Python"