Osho
Osho

Reputation: 168

xml-diff - grabbing data by tag

I have a parsed little XML file:

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

And this code:

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)

And here is an effect:

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

I am receiving NONEs instead of INTs or some value of diff:update tag.

How to receive value of diff:update which is added by xml-diff library?

Upvotes: 0

Views: 159

Answers (1)

AvyChanna
AvyChanna

Reputation: 336

As stated here, namespaces are expanded from diff:update-attr to {http://....}update-attr. Use that to get your resource. Also, if a default namespace is given, you will have to edit your find* methods

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)

Upvotes: 1

Related Questions