XML parser for Python to get tags before and after matching tag -


i want write xml parser in python checking of test cases failing , if failing, check error message. want extract part of test case failing along other tags. tried using beautiful-soup. couldn't succeed properly. below sample of xml file:

<test>     <test_str>test case #1: check login</test_str>     <testcase>         <pass_status>false</pass_status>         <criticality>true</criticality>         <errormsg>invalid credentials: check login credentials </errormsg>         <tags>             <string>test01</string>         </tags>         <parent reference="../../../.."/>         <failedsince>0</failedsince>     </testcase> </test> <test>     <test_str>test case 02:pass valid credentials selecting valid username</test_str>     <testcase>         <pass_status>true</pass_status>         <criticality>true</criticality>         <tags>             <string>test01</string>         </tags>         <parent reference="../../../.."/>         <failedsince>0</failedsince>     </testcase> </test> 

you want use xml parser, not write one.

you may want wrap whole xml <tests>...</tests> tags make life bit easier:

string = '''<tests>             <test>                 <test_str>test case #1: check login</test_str>                 <testcase>                     <pass_status>false</pass_status>                     <criticality>true</criticality>                     <errormsg>invalid credentials: check login credentials </errormsg>                     <tags>                         <string>test01</string>                     </tags>                     <parent reference="../../../.."/>                     <failedsince>0</failedsince>                 </testcase>             </test>             <test>                 <test_str>test case 02:pass valid credentials selecting valid username</test_str>                 <testcase>                     <pass_status>true</pass_status>                     <criticality>true</criticality>                     <tags>                         <string>test01</string>                     </tags>                     <parent reference="../../../.."/>                     <failedsince>0</failedsince>                 </testcase>             </test> </tests>''' 

then:

import xml.etree.elementtree et  xml_node = et.fromstring(string)  test_node in xml_node.findall('test'):     test_case_node = test_node.find('testcase')     pass_status_node = test_case_node.find('pass_status')     if pass_status_node.text == 'false':         print(test_case_node.find('errormsg').text) 

this outputs:

'invalid credentials: check login credentials' 

in order read file replace xml_node = et.fromstring(string) with:

with open('file/path') f:     xml_node = et.fromstring(f.read()) 

Comments

Popular posts from this blog

python - Selenium remoteWebDriver (& SauceLabs) Firefox moseMoveTo action exception -

html - How to custom Bootstrap grid height? -

transpose - Maple isnt executing function but prints function term -