0

我是 Python 新手。我正在尝试在 python 中解析下面的 XML 以获取测试摘要。

<?xml version="1.0"?>
<SquishReport version="2.1">
  <test name="HMI_testing">
    <prolog time="2013-01-18T14:41:09+05:30"/>
    <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 property comparison of ':_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 name="tst_Setup_Menu"> <prolog time="2013-01-18T14:41:11+05:30"/>
      <verification name="VP1" file="D:/Squish/HMI_testing/tst_Setup_Menu/test.py" type="screenshot" line="6">
        <result time="2013-01-18T14:41:12+05:30" type="PASS">
          <description>VP1: Screenshot comparison of ':_QMenu_3' passed</description>
          <description type="DETAILED">Screenshots are considered identical</description>
          <description type="object">:_QMenu_3</description>
          <description type="failedImage">Screenshots are considered identical</description>
        </result>
      </verification>
      <verification name="VP2" file="D:/Squish/HMI_testing/tst_Setup_Menu/test.py" type="screenshot" line="8">
        <result time="2013-01-18T14:41:16+05:30" type="FAIL">
          <description>VP2: Screenshot comparison of ':_QMenu_3' failed</description>
          <description type="DETAILED">Screenshots do not match. Differing screenshot saved as 'D:/Squish/HMI_testing/tst_Setup_Menu/verificationPoints\failedImages\failed_1.png'</description>
          <description type="object">:_QMenu_3</description>
          <description type="failedImage">D:/Squish/HMI_testing/tst_Setup_Menu/verificationPoints\failedImages\failed_1.png</description>
        </result>
      </verification>
      <verification name="VP3" file="D:/Squish/HMI_testing/tst_Setup_Menu/test.py" type="screenshot" line="9">
        <result time="2013-01-18T14:41:20+05:30" type="PASS">
          <description>VP3: Screenshot comparison of ':_QMenu_4' passed</description>
          <description type="DETAILED">Screenshots are considered identical</description>
          <description type="object">:_QMenu_4</description>
          <description type="failedImage">Screenshots are considered identical</description>
        </result>
      </verification>
      <epilog time="2013-01-18T14:41:28+05:30"/>
    </test>
    <epilog time="2013-01-18T14:41:28+05:30"/>
  </test>
</SquishReport>

我尝试了下面的代码,但我无法根据我的需要进行解析

import sys
import xml.dom.minidom as XY
#import xml.etree.ElementTree as ET

file = open("result.txt", "w")
tree = XY.parse('D:\\Squish\\squish results\\Results-On-2013-01-18_0241 PM.xml')
elem = tree.getElementsByTagName('test')
variable = elem[0].firstChild.data
print (variable)

我想做的是,从第一个“测试”标签中提取“HMI_testing”,“测试名称”tst_case1,并从xml文件中通过/失败

4

2 回答 2

1

我推荐使用 Xpath,我已经用过好几次了,而且总是很方便而且很有帮助: Xpath 教程。它也很容易使用。

于 2013-01-18T15:40:39.437 回答
1

代替

variable = elem[0].firstChild.data

利用

variable = elem[0].getAttribute('name')

您不是在寻找子元素,而是在寻找属性name

于 2013-01-18T15:47:28.980 回答