Osho
Osho

Reputation: 168

How to parse XML file without deleting tags?

I have a little xml-parsing problem.

Here is my junit-1.xml file:

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

Here is junit-2.xml file:

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

And the last one junit-diff.xml with differences between two above:

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

What I need to do and how to parse it to have an output like this below - so I am trying to just format somehow 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"
...
...

I want to compare two application versions using xml-diff library and for now my code looks like this:

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

After adding write() method like here:

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

I am receiving <_io.BufferedReader name='junit-diff.xml'>,

So It has no effect. What should I change?

Upvotes: 1

Views: 127

Answers (1)

Osho
Osho

Reputation: 168

We can now close this post. Here is the solution that I made:

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)

Upvotes: 1

Related Questions