0

我有一个解析的小 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

我收到 NONEs 而不是 INTs 或一些diff:update标签值。

如何接收图书馆diff:update添加的价值?xml-diff

4

1 回答 1

1

如此处所述,名称空间从 扩展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)
于 2021-02-21T18:39:31.077 回答