Home > database >  How to check existence or nonexistence of a tag in xml using Python
How to check existence or nonexistence of a tag in xml using Python

Time:08-10

I have an XML file parsed with xml.etree.ElementTree in python with this kind of structure:

<?xml version="1.0" encoding="UTF-8"?>
<testsuites>
    <testsuite name="force.apex" timestamp="2022-08-01T11:13:33.672Z" hostname="***" tests="192" failures="21"  errors="0"  time="371.30">
<testcase name="wavewrapTest" classname="SVC_WaveInsightGeneratorDatasetVerTest" time="0.10">
        </testcase>
        <testcase name="testSVC_UpdateLastTransactionDateTest" classname="SVC_UpdateLastTransactionDateTest" time="2.65">
        </testcase>
        <testcase name="wavewrapTest" classname="SVC_WaveInsightJSONResponseHandlerTest" time="0.10">
        </testcase>
        <testcase name="testFeedComment" classname="DataMaskFeedItemTest" time="2.05">
            <failure message="System.NullPointerException: Argument cannot be null."><![CDATA[Class.SVC_AccountQueueableHashedId.execute: line 15, column 1]]></failure>
        </testcase>
        <testcase name="testFeedItem" classname="DataMaskFeedItemTest" time="0.76">
            <failure message="System.NullPointerException: Argument cannot be null."><![CDATA[Class.SVC_AccountQueueableHashedId.execute: line 15, column 1]]></failure>
        </testcase>
        <testcase name="schedulerTest" classname="pi_scheduleBirthdayTaskTest" time="0.09">
            <failure message="System.QueryException: List has no rows for assignment to SObject"><![CDATA[Class.pi_scheduleBirthdayTaskTest.schedulerTest: line 19, column 1]]></failure>
        </testcase>
        <testcase name="testDefaultSchedule" classname="SVC_AMLBatchTest" time="0.15">
            <failure message="System.AsyncException: Based on configured schedule, the given trigger 'SCHEDULED_APEX_JOB_TYPE.000000000000000' will never fire."><![CDATA[Class.SVC_AMLBatchTest.testDefaultSchedule: line 228, column 1]]></failure>
        </testcase>
    </testsuite>
</testsuites>

You can see that some <testcase> tags have <failure> tag and some have not. I need a solution to get only the ones which have the <failure> tag with message attribute and need to look something like this: Class <classname> failed with message <message>. More exactly, I need to skip the ones that don't have failure tag. Here is what I tried until now:

for item in root.findall(".//testcase"):
    failure = item.find('failure')
    if failure is None:
        continue
    failures = failure.text
    for item1 in root.findall(".//testcase/failure"):
        children = item1.get('message')
    print("Class: {} failed with message: {}".format(failures, children))
for item in root.iter('testcase'):
    for item1 in root.iter('failure'):
        print(item.attrib['classname'], item1.attrib['message'])

CodePudding user response:

If I understand you correctly, something like this should work:

for test in root.findall('.//testcase[failure]'):
  classname = test.attrib['classname']
  message = test.find('./failure').attrib['message']
  print(f"Class: {classname} failed with message: {message}")

Outuput:

Class: DataMaskFeedItemTest failed with message: System.NullPointerException: Argument cannot be null.
Class: DataMaskFeedItemTest failed with message: System.NullPointerException: Argument cannot be null.
Class: pi_scheduleBirthdayTaskTest failed with message: System.QueryException: List has no rows for assignment to SObject
Class: SVC_AMLBatchTest failed with message: System.AsyncException: Based on configured schedule, the given trigger 'SCHEDULED_APEX_JOB_TYPE.000000000000000' will never fire.
  • Related