如何使用python解析XML

编程入门 行业动态 更新时间:2024-10-09 03:24:18
本文介绍了如何使用python解析XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用python解析xml以创建结果摘要文件.下面是我的代码和xml片段,就像下面的一样,我有几个部分分别为<test>和</test>

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 <test> and </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>部分中,有多少个通过/失败.

What I want to get is, in one <test> section how many PASS / FAIL is there.

使用下面的代码在xml文件中打印总的通过/失败.但是我对每个部分中有多少个通过/失败感兴趣.有哪个男孩能告诉我提取这个东西的程序吗?

With the below code its printing the total pass/Fail in the xml file.But i am interested in each section how many PASS/FAIL. can any boy tell me the procedure to fetchout this ?

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))

推荐答案

我不会为此任务使用minidom; DOM API非常繁琐,冗长,不适合搜索和匹配.

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

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

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}

上面的代码对每个<test>元素的通过和失败测试的次数进行计数,并将它们存储在字典中,该字典的键为<test>元素的name属性.

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.

我已经使用Python 3.2和您发布的另一个问题中的完整XML文档,结果为:

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}}

更多推荐

如何使用python解析XML

本文发布于:2023-11-28 06:08:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1641236.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   python   XML

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!