Parsing custom xml file using python

88 Views Asked by At

I have an xml file of following format :

<?xml version='1.0' encoding='utf-8'?>
<execute time="0.59">
<exec name="recursive_a" loops="3" fail="2" skipped="0">
<testcase tname="test_a" name="test.cpp" time="0.50">
<pass>
001,test,pass
</pass>
</testcase>
</exec>
</execute>

how can i parse "recursive_a" string from this xml using python? (i am using minidom xml parser)

1

There are 1 best solutions below

0
Hermann12 On

With xml.etree.ElementTree and pandas one solution could be:

import xml.etree.ElementTree as ET
import pandas as pd

tree = ET.parse('Code3r.xml')
root = tree.getroot()

for elem in root:
    if elem.tag == "exec":
        # print(elem.attrib) or with pandas
        df = pd.DataFrame.from_dict(elem.attrib, orient='index')
        print(df.T.to_string(index=False))

Output:

       name loops fail skipped
recursive_a     3    2       0