XML解析以使用minidom使用python获取描述 [英] XML parsing to get description using python using minidom

查看:25
本文介绍了XML解析以使用minidom使用python获取描述的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请建议我需要为以下代码添加哪些附加代码,以便我可以解析以下 XML 代码以获取描述.

Please suggest me what add additional code need to be added for the below code so that i can parse the below XML code to get the description.

<SquishReport version="2.1">
    <test name="HMI_testing">
        <prolog time="2013-01-22T18:59:43+05:30"/>
        <test name="tst_Setup_menu_2">
            <prolog time="2013-01-22T18:59:43+05:30"/>
            <verification line="7" type="" file="D:/Squish/HMI_testing/tst_Setup_menu_2/test.py" name="ECG is enabled">
                <result type="PASS" time="2013-01-22T18:59:45+05:30">
                    <description>Comparison</description>
                    <description type="DETAILED">'1' and 'True' are equal</description>
                    <description type="DETAILED">ECG is enabled</description>
                </result>
            </verification>
            <verification line="9" type="" file="D:/Squish/HMI_testing/tst_Setup_menu_2/test.py" name="ECG is enabled">
                <result type="PASS" time="2013-01-22T18:59:45+05:30">
                    <description>Comparison</description>
                    <description type="DETAILED">'1' and 'True' are equal</description>
                    <description type="DETAILED">ECG is enabled</description>
                </result>
            </verification>
            <verification line="11" type="" file="D:/Squish/HMI_testing/tst_Setup_menu_2/test.py" name="P1 is disabled">
                <result type="PASS" time="2013-01-22T18:59:45+05:30">
                    <description>Comparison</description>
                    <description type="DETAILED">'0' and 'False' are equal</description>
                    <description type="DETAILED">P1 is disabled</description>
                </result>
            </verification>
            <verification line="13" type="" file="D:/Squish/HMI_testing/tst_Setup_menu_2/test.py" name="P2 is disabled">
                <result type="PASS" time="2013-01-22T18:59:45+05:30">
                    <description>Comparison</description>
                    <description type="DETAILED">'0' and 'False' are equal</description>
                    <description type="DETAILED">P2 is disabled</description>
                </result>
            </verification>
            <verification line="15" type="" file="D:/Squish/HMI_testing/tst_Setup_menu_2/test.py" name="SPO2 is enabled">
                <result type="PASS" time="2013-01-22T18:59:45+05:30">
                    <description>Comparison</description>
                    <description type="DETAILED">'1' and 'True' are equal</description>
                    <description type="DETAILED">SPO2 is enabled</description>
                </result>
            </verification>
            <verification line="17" type="" file="D:/Squish/HMI_testing/tst_Setup_menu_2/test.py" name="CO2 is disabled">
                <result type="PASS" time="2013-01-22T18:59:45+05:30">
                    <description>Comparison</description>
                    <description type="DETAILED">'0' and 'False' are equal</description>
                    <description type="DETAILED">CO2 is disabled</description>
                </result>
            </verification>
            <verification line="19" type="" file="D:/Squish/HMI_testing/tst_Setup_menu_2/test.py" name="RESP is disabled">
                <result type="PASS" time="2013-01-22T18:59:45+05:30">
                    <description>Comparison</description>
                    <description type="DETAILED">'0' and 'False' are equal</description>
                    <description type="DETAILED">RESP is disabled</description>
                </result>
            </verification>
            <verification line="21" type="" file="D:/Squish/HMI_testing/tst_Setup_menu_2/test.py" name="TEMP is disabled">
                <result type="PASS" time="2013-01-22T18:59:45+05:30">
                    <description>Comparison</description>
                    <description type="DETAILED">'0' and 'False' are equal</description>
                    <description type="DETAILED">TEMP is disabled</description>
                </result>
            </verification>
            <epilog time="2013-01-22T18:59:45+05:30"/>
        </test>
        <epilog time="2013-01-22T18:59:45+05:30"/>
    </test>
</SquishReport>

我需要打印的是 ECG 已启用、NIBP 已启用等.

what i need to print is ECG is Enabled , NIBP is enabled etc..

我使用的代码添加在下面.由于某些依赖性,我需要更新相同的代码.需要在代码中提到的print处添加代码(这里需要添加代码)

the code i used is added below. I need to update the same code because of some dependency. need to add the code at print(Need to add the code here) mentioned in the code

import sys
import xml.dom.minidom as XY

file = open("Result_Summary.txt", "w")
tree = XY.parse('Results-On-2013-01-22_0659.xml')
#print (str(sys.argv[1]))
#tree = XY.parse(sys.argv[1])

Test_name = tree.getElementsByTagName('test')
count_testname =0
    file.write(' -----------------------------------------------------------------------------------------------------\n\n')
file.write('\tTest Name \t\t No Of PASS\t\t No Of FAIL\t\t\t Description\t\t \n')
file.write(' -----------------------------------------------------------------------------------------------------\n\n')
for my_Test_name in Test_name:
    count_testname = count_testname+1
    my_Test_name_final = my_Test_name.getAttribute('name')
    if(count_testname > 1):
        #print(my_Test_name_final)
        file.write(my_Test_name_final)
        file.write('\t\t\t')
        my_Test_status = my_Test_name.getElementsByTagName('result')
        passcount = 0
        failcount = 0
        for my_Test_status_1 in my_Test_status:
            my_Test_description = my_Test_name.getElementsByTagName('description')
            for my_Test_description_1 in my_Test_description:
                my_Test_description_final = my_Test_description_1.getAttribute('type')
                print(Need to add the code here)
                my_Test_status_final = my_Test_status_1.getAttribute('type')
                if(my_Test_status_final == 'PASS'):
                   passcount = passcount+1
                if(my_Test_status_final == 'FAIL'):
                   failcount = failcount+1
            #print(str(my_Test_status_final))
        file.write(str(passcount))
        #print(passcount)
        file.write('\t\t\t')
        file.write(str(failcount))

pected result

tst_Setup_menu_2     8        0        ECG Enabled
                                       p1 Enabled
                                       P2 Enabled etc

推荐答案

扩展我的 以前的答案,请务必将 ElementTree API 用于此类任务:

Expanding on my previous answer, please do use the ElementTree API for such tasks:

from xml.etree import ElementTree as ET

tree = ET.parse(r'D:\Squish\squish results\Results-On-2013-01-18_0241 PM.xml')

with open("Result_Summary.txt", "w") as output:
    output.write(' {} \n\n'.format('-' * 101))
    output.write('\tTest Name \t\t No Of PASS\t\t No Of FAIL\t\t\t Description\t\t \n')
    output.write(' {} \n\n'.format('-' * 101))

    # Find all <test> elements with a <verification> child:
    for test in tree.findall('.//test[verification]'):
        # Collect passed and failed counts
        passed = len(test.findall(".//result[@type='PASS']"))
        failed = len(test.findall(".//result[@type='FAIL']"))
        # Collect all the *last* <description> elements of type DETAILED
        descriptions = test.findall(".//result/description[@type='DETAILED'][last()]")
        # write a line of information to the file, including first desc
        output.write('{0}\t\t\t{1}\t\t\t{2}\t\t\t{3}\n'.format(
            test.attrib['name'], passed, failed, descriptions[0].text))
        # write remaining descriptions
        for desc in descriptions[1:]:
            output.write('\t\t\t\t\t\t\t\t\t{0}\n'.format(desc.text))

这篇关于XML解析以使用minidom使用python获取描述的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆