xml-diff - 通过标签抓取数据

xml-diff - grabbing data by tag

我有一个经过解析的小 XML 文件:

<testsuite xmlns:diff="http://namespaces.shoobx.com/diff" name="Performance Timings" tests="9" errors="0" failures="0" skipped="0" time="4338.381" diff:update-attr="time:4497.381">
<testcase classname="Performance Timings" name="Execution time for switching MAIN BUTTONS" time="498.472" diff:update-attr="time:568.473">
</testcase>
<testcase classname="Performance Timings" name="Execution time for switching between METER MANAGEMENT tabs" time="885.210" diff:update-attr="time:989.230">
</testcase>
<testcase classname="Performance Timings" name="Execution time for switching between ANALYSIS tab" time="55.173" diff:update-attr="time:60.178">
</testcase>
<testcase classname="Performance Timings" name="Execution time for LOADING CIRCLE" time="1140.191" diff:update-attr="time:1040.298">
</testcase>
<testcase classname="Performance Timings" name="Execution time for creating more than one thing" time="327.563" diff:update-attr="time:427.563">
</testcase>
<testcase classname="Performance Timings" name="Execution time for switching between tabs in ANALYSIS WIZARD" time="7.202" diff:update-attr="time:7.809">
</testcase>
<testcase classname="Performance Timings" name="Change configuration settings" time="32.111" diff:update-attr="time:33.919">
</testcase>
<testcase classname="Performance Timings" name="Execution time for changing METER DETAILS (WATER) page and navigation to CHARTS" time="25.326" diff:update-attr="time:28.764">
</testcase>
<testcase classname="Performance Timings" name="Execution time for changing METER DETAILS (ENERGY) page and navigation to CHARTS" time="36.651" diff:update-attr="time:36.172">
</testcase>
</testsuite>

还有这段代码:

from xml.etree import cElementTree as ET
with open('junit-diff.xml', 'rb') as junit_diff1:
tree = ET.parse('junit-diff.xml')
root = tree.getroot()

for type_tag in root.findall('testcase'):
    tc_name = type_tag.get('name')
    exec_time = type_tag.get('time')
    diff_time_type = type_tag.get('diff:update') #here is a problem
    str_is = ' is '
    print(tc_name + str_is + exec_time, diff_time_type)

这是一个效果:

Execution time for switching MAIN BUTTONS is 498.472 None
Execution time for switching between METER MANAGEMENT tabs is 885.210 None
Execution time for switching between ANALYSIS tab is 55.173 None
Execution time for LOADING CIRCLE is 1140.191 None
Execution time for creating more than one thing is 327.563 None
Execution time for switching between tabs in ANALYSIS WIZARD is 7.202 None
Change configuration settings is 32.111 None
Execution time for changing METER DETAILS (WATER) page and navigation to CHARTS is 25.326 None
Execution time for changing METER DETAILS (ENERGY) page and navigation to CHARTS is 36.651 None

我收到的是 NONE 而不是 INT 或 diff:update 标签的某个值。

如何接收由xml-diff库添加的diff:update的值?

here 所述,命名空间从 diff:update-attr 扩展到 {http://....}update-attr。用它来获取你的资源。此外,如果给出了默认名称空间,您将必须编辑 find* 方法

from xml.etree import cElementTree as ET
tree = ET.parse('test.xml')
root = tree.getroot()
namespaces = {"diff": "http://namespaces.shoobx.com/diff"}
for type_tag in root.findall('testcase'):
    tc_name = type_tag.get('name')
    exec_time = type_tag.get('time')
    diff_time_type = type_tag.get('{http://namespaces.shoobx.com/diff}update-attr')
    str_is = 'is'
    print(tc_name, str_is, exec_time, diff_time_type)