0

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>

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

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))

Ex

pected result

tst_Setup_menu_2     8        0        ECG Enabled
                                       p1 Enabled
                                       P2 Enabled etc
18
  • 2
    Use 'view source' before copying XML from Firefox; the browser displays the XML with some extra widgets that show up as - signs when you paste it here otherwise. Commented Jan 22, 2013 at 14:45
  • Please, try using ElementTree some more. You never provided me with enough information to help you with your previous question where I showed you that ElementTree is far easier to use for these tasks. Commented Jan 22, 2013 at 14:46
  • Whats the dependency? You should really consider refactoring and possibly switching to another parsing library, because what you have is utterly unreadable. Commented Jan 22, 2013 at 14:48
  • @Martijn Pieters : i am using PythonWin 3.2.2 and it was not able to find lxml Commented Jan 22, 2013 at 15:05
  • @BrijeshKrishnan: ElementTree is part of Python, it comes bundled with Python 3.2 as xml.etree.ElementTree. The lxml package uses the same API but is not required for your tasks, I'd say. Commented Jan 22, 2013 at 15:07

2 Answers 2

1

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))
Sign up to request clarification or add additional context in comments.

6 Comments

last tab i am getting as <Element 'description' at 0x42ce438>
tst_Setup_menu_2 8 0 <Element 'description' at 0x42ce438> <Element 'description' at 0x42ce160> <Element 'description' at 0x42d5240> <Element 'description' at 0x42d5ef0> <Element 'description' at 0x42d75c0> <Element 'description' at 0x42d7b00> <Element 'description' at 0x42cc160> <Element 'description' at 0x42ccef0>
@BrijeshKrishnan: oops, sorry, that should be .text behind each element.
Although this might solve the problem, it doesn't really answer the question. Does it really deserve an accept in that case?
@MarkRansom: The conversation has been going for a more than a few questions now. The OP appeared to be confused as to the availability of ElementTree, and had trouble running previous answers using ET. Moreover, I usually only suggest accepting an answer when the OP a) thanks me for my help and b) has not yet accepted an answer before.
|
0

The following looks for result nodes and then the descriptions contained within them. This prevents incorrect description nodes from being selected

import xml.dom.minidom
# str = your_string_from_the_question

doc = xml.dom.minidom.parseString(str)
for result in doc.getElementsByTagName("result"):
    for description in result.getElementsByTagName("description"):
        print description.firstChild.data

Gives

Comparison
'1' and 'True' are equal
ECG is enabled
....

The getElementsByName method returns a list of nodes (hence the looping). The line result.getElementsByTagName("description") returns the node <description>Comparison</description>. The text 'Comparison' is a text node in XML, hence you have to reference it with firstChild (to get the node) and data to get the text

[EDIT]

If you require the 3rd description (and you know it will always be the third) you could do this (untested)

doc = xml.dom.minidom.parseString(str)
for result in doc.getElementsByTagName("result"):
    description_list = result.getElementsByTagName("description")
    if len(description_list.length >= 3):
        description[2].firstChild.data

2 Comments

thanks. that works. but a question here i am in need of only the third description. any provision to fetch only that "ECG is enabled "?
when i put if len(my_Test_description >= 3): it gives error Traceback (most recent call last): TypeError: unorderable types: NodeList() >= int()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.