如何使用python解析XML

时间:2013-01-20 12:16:09

标签: python xml parsing xml-parsing

我正在尝试使用python解析xml以创建结果摘要文件。下面是我的代码和xml片段,如下所示,我有几个部分<test></test>

<test name="tst_case1">
  <prolog time="2013-01-18T14:41:09+05:30"/> 
  <verification name="VP5" file="D:/Squish/HMI_testing/tst_case1/test.py" type="properties" line="6"> 
    <result time="2013-01-18T14:41:10+05:30" type="PASS"> 
      <description>VP5: Object propertycomparisonof ':_QMenu_3.enabled'passed</description>        <description type="DETAILED">'false' and 'false' are equal</description> 
      <description type="object">:_QMenu_3</description> 
      <description type="property">enabled</description> 
      <description type="failedValue">false</description> 
    </result> 
  </verification>
  <epilog time="2013-01-18T14:41:11+05:30"/> 
</test>

我想要的是, 在一个<test>部分中,有多少通行/失败。

使用下面的代码打印xml文件中的总通过/失败。但我感兴趣的是每个部分有多少通过/未通过。任何男孩都可以告诉我取出这个的程序吗?

import sys
import xml.dom.minidom as XY

file = open("result.txt", "w")
tree = XY.parse('D:\\Squish\\squish results\\Results-On-2013-01-18_0241 PM.xml')
Test_name = tree.getElementsByTagName('test')
Test_status = tree.getElementsByTagName('result')
count_testname =0
passcount = 0
failcount = 0
Test_name_array = []
for my_Test_name in Test_name:
    count_testname = count_testname+1
    passcount = 0
    failcount = 0
    my_Test_name_final = my_Test_name.getAttribute('name')
    Test_name_array = my_Test_name_final
    if(count_testname > 1):
        print(my_Test_name_final)
        for my_Test_status in Test_status:
            my_Test_status_final = my_Test_status.getAttribute('type')
            if(my_Test_status_final == 'PASS'):
               passcount = passcount+1
            if(my_Test_status_final == 'FAIL'):
               failcount = failcount+1
            print(str(my_Test_status_final))

4 个答案:

答案 0 :(得分:2)

我不会使用minidom完成这项任务; DOM API非常麻烦,冗长,不适合搜索和匹配。

Python库还包含xml.etree.ElementTree API,我会改用它:

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}

上面的代码计算每个<test>元素的传递和失败测试的数量,并将它们存储在字典中,并键入name元素的<test>属性。

我已经使用Python 3.2和您发布的另一个问题的full XML document测试了上述代码,结果是:

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

答案 1 :(得分:0)

感谢发布。我用minidon工作了。 仍然希望看到如何使用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 = 0
        for 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+1
            if(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()

答案 2 :(得分:0)

虽然不是标准模块但值得安装的努力是lxml,特别是如果你想进行快速Xml解析等恕我直言。

如果没有完整的结果示例,我会猜到它们的样子。

from lxml import etree

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

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

for test in 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")

我根据我对你的xml的猜测产生了以下运行,这可以让你知道发生了什么。

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

我喜欢的lxml是多么有表现力,YMMV。

答案 3 :(得分:0)

我看到你正在使用Squish。你应该检查 \ examples \ regressiontesting 下的压扁文件夹。在那里,您可以找到名为 xml2result2html.py 的文件。在这里,您可以找到将挤压测试结果转换为html的示例。