Reputation: 370
How to parse the following XML to give me output as @test : Error Msg?
XML
<?xml version='1.0' encoding='UTF-8'?>
<featureResults>
<feature>
<comments/>
<keyword>Feature</keyword>
<name>Test Feature</name>
<line>2</line>
<description></description>
<tags>
<Tag>
<name>@test</name>
<line>1</line>
</Tag>
</tags>
<id>Test feature</id>
</feature>
<uri>Test.feature</uri>
<scenarioResults>
<scenario>
<comments/>
<keyword>Scenario Outline</keyword>
<name>Login-logout from Test</name>
<line>11</line>
<description></description>
<tags>
<Tag>
<name>@test</name>
<line>1</line>
</Tag>
<Tag>
<name>@test1</name>
<line>4</line>
</Tag>
</tags>
<id>Test-feature;login-logout-from-Test;;2</id>
<type>scenario</type>
</scenario>
<steps>
<StepResult>
<step>
<comments/>
<keyword>Given </keyword>
<name>navigate to "Hello"</name>
<line>6</line>
</step>
<result>
<status>failed</status>
<duration>90475603939</duration>
<error__message>Error Msg </error__message>
</result>
</scenario>
</StepResult>
</ScenarioResult>
Here is the Python script I tried using lxml element tree but it gives me null value on print failures:
failures = {}
doc = etree.parse(os.path.join(Given.xml))
root = doc.getroot()
for case in root.findall(".//ScenarioResult"):
for test in case.findall(".//tags"):
test.find(".//name").text
error__message = case.find("error__message")
if error__message is None:
continue
failures[name] = (error__message.text[:200] + '..') if len(error__message.text) > 200 else error__message.text
Any help would be appreciated Thanks.
Upvotes: 1
Views: 90
Reputation: 58
This Should work as xml is not formated properly need to find relative path
failures = {}
try:
doc = etree.parse(os.path.join(resultsDir,detailSummaryFile))
root = doc.getroot()
for case in root.findall(".//scenarioResults"):
name = case.find(".//Tag/name").text
error__message = case.find(".//error__message")
if error__message is None:
continue
failures[name] = (error__message.text[:200] + '..') if len(error__message.text) > 200 else error__message.text
return failures
except (Exception,IOError), e:
return {
"success": False,
"error":str(e)
}
Upvotes: 1
Reputation: 5658
It appears that you xml is badly formed. With fixes:
<?xml version='1.0' encoding='UTF-8'?>
<featureResults>
<feature>
<comments/>
<keyword>Feature</keyword>
<name>Test Feature</name>
<line>2</line>
<description></description>
<tags>
<Tag>
<name>@test</name>
<line>1</line>
</Tag>
</tags>
<id>Test feature</id>
</feature>
<uri>Test.feature</uri>
<scenarioResults>
<scenario>
<comments/>
<keyword>Scenario Outline</keyword>
<name>Login-logout from Test</name>
<line>11</line>
<description></description>
<tags>
<Tag>
<name>@test</name>
<line>1</line>
</Tag>
<Tag>
<name>@test1</name>
<line>4</line>
</Tag>
</tags>
<id>Test-feature;login-logout-from-Test;;2</id>
<type>scenario</type>
</scenario>
<steps>
<StepResult>
<scenario>
<step>
<comments/>
<keyword>Given </keyword>
<name>navigate to "Hello"</name>
<line>6</line>
</step>
<result>
<status>failed</status>
<duration>90475603939</duration>
<error__message>Error Msg </error__message>
</result>
</scenario>
</StepResult>
</steps>
</scenarioResults>
</featureResults>
there is a module that can convert xml to dictionary. do pip install xmltodict
import xmltodict
with open('1.xml') as fd:
obj = xmltodict.parse(fd.read())
print(obj['featureResults']['feature']['tags']['Tag']['name'], end=" ")
print(obj['featureResults']['scenarioResults']['steps']['StepResult']['scenario']['result']['error__message'])
@test Error Msg
Upvotes: 0
Reputation: 37
I think you can use regex to filter the tags in xml and get the content which you want. I write a little python code for you to do this. May it can help you.
re_a = re.compile('</?\w+.*?>',re.DOTALL)
re_b = re.compile('<\?.*?\?>')
s = re_a.sub('',your_xml)
s = re_b.sub('',s)
s = re.sub('\\t','',s)
s = re.sub(' ','',s)
s = re.sub('\\n+','\\n',s)
print(s)
the result is as follow:
Feature
TestFeature
2
@test
1
Testfeature
Test.feature
ScenarioOutline
Login-logoutfromTest
11
@test
1
@test1
4
Test-feature;login-logout-from-Test;;2
scenario
Given
navigateto"Hello"
6
failed
90475603939
ErrorMsg
Upvotes: 0