1

我有一点 xml 解析问题。

这是我的 junit-1.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<testsuite name="Performance Timings" tests="9" errors="0" failures="0" skipped="0" time="4497.381">
<testcase classname="Performance Timings" name="Execution time for switching MAIN BUTTONS" time="568.473">
</testcase>
<testcase classname="Performance Timings" name="Execution time for switching between METER MANAGEMENT tabs" time="989.230">
</testcase>
<testcase classname="Performance Timings" name="Execution time for switching between ANALYSIS tab" time="60.178">
</testcase>
<testcase classname="Performance Timings" name="Execution time for LOADING CIRCLE" time="1040.298">
</testcase>
<testcase classname="Performance Timings" name="Execution time for creating more than one thing" time="427.563">
</testcase>
<testcase classname="Performance Timings" name="Execution time for switching between tabs in ANALYSIS WIZARD" time="7.809">
</testcase>
<testcase classname="Performance Timings" name="Change configuration settings" time="33.919">
</testcase>
<testcase classname="Performance Timings" name="Execution time for changing METER DETAILS (WATER) page and navigation to CHARTS" time="28.764">
</testcase>
<testcase classname="Performance Timings" name="Execution time for changing METER DETAILS (ENERGY) page and navigation to CHARTS" time="36.172">
</testcase>
</testsuite>

这是 junit-2.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<testsuite name="Performance Timings" tests="9" errors="0" failures="0" skipped="0" time="4338.381">
<testcase classname="Performance Timings" name="Execution time for switching MAIN BUTTONS" time="498.472">
</testcase>
<testcase classname="Performance Timings" name="Execution time for switching between METER MANAGEMENT tabs" time="885.210">
</testcase>
<testcase classname="Performance Timings" name="Execution time for switching between ANALYSIS tab" time="55.173">
</testcase>
<testcase classname="Performance Timings" name="Execution time for LOADING CIRCLE" time="1140.191">
</testcase>
<testcase classname="Performance Timings" name="Execution time for creating more than one thing" time="327.563">
</testcase>
<testcase classname="Performance Timings" name="Execution time for switching between tabs in ANALYSIS WIZARD" time="7.202">
</testcase>
<testcase classname="Performance Timings" name="Change configuration settings" time="32.111">
</testcase>
<testcase classname="Performance Timings" name="Execution time for changing METER DETAILS (WATER) page and navigation to CHARTS" time="25.326">
</testcase>
<testcase classname="Performance Timings" name="Execution time for changing METER DETAILS (ENERGY) page and navigation to CHARTS" time="36.651">
</testcase>
</testsuite>

最后一个 junit-diff.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>

我需要做什么以及如何解析它以获得如下所示的输出 - 所以我试图以某种方式格式化 junit-diff.xml?

Performance Timings" tests="9" errors="0" failures="0" skipped="0" time="4497.381"
Execution time for switching MAIN BUTTONS time="568.473"
Execution time for switching between METER MANAGEMENT tabs time="989.230"
Execution time for switching between ANALYSIS tab time="60.178"
...
...

我想使用 xml-diff 库比较两个应用程序版本,现在我的代码如下所示:

from lxml import etree
import xml.etree.ElementTree as ET
from xmldiff import main, formatting


diff = main.diff_files('junit-1.xml', 'junit-2.xml',
                       formatter=formatting.XMLFormatter())

with open('junit-diff.xml', 'w') as diff_report:
    diff_report.write(diff)


with open('junit-diff.xml', 'rb') as difff:
    tree = ET.parse('junit-diff.xml')
    root = tree.getroot()
    test_cases = root.findall('.//testcase')
    for test_case in test_cases:
        test_case.attrib['testcase'] = test_case.find('./testcase classname')

添加write()方法后,如下所示:

with open('sec_diff.xml', 'w') as sec:
    sec.write(str(difff))

我正在接收<_io.BufferedReader name='junit-diff.xml'>

所以它没有效果。我应该改变什么?

4

1 回答 1

1

我们现在可以关闭这篇文章了。这是我提出的解决方案:

from xml.etree import cElementTree as ET
from xmldiff import main, formatting

diff = main.diff_files('junit-1.xml', 'junit-2.xml',
                       formatter=formatting.XMLFormatter())

with open('junit-diff.xml', 'w') as junit_diff:
    junit_diff.write(diff)

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')
        str_is = ' is '
        print(tc_name + str_is + exec_time)
于 2021-02-18T12:04:47.507 回答