RandomDisplayName
RandomDisplayName

Reputation: 281

Get CDATA using xml.etree.ElementTree

I have the following xml:

<?xml version="1.0" ?>
<testsuite errors="1" failures="0" name="test" tests="1" time="3.393">
        <testcase classname="test" name="case time="3.393">
                <error message="'test' object has no attribute 'test'" type="AttributeError">
<![CDATA["HERES THE DETAILED ERROR"]]>             </error>
        </testcase>
        <system-out>
<![CDATA["ANOTHER TEXT"]]>
 </system-out>
        <system-err>
<![CDATA[]]>    </system-err>
</testsuite>

and I have the following parsing code:

tree = ET.parse("test-reports/{0}".format(resultfile))
test = tree.getroot()
for child in test:
    if child.tag == "testcase":
        if child.find("error") is not None:
            state = 2
            # How do I get <error>s CDATA here?
        else:
            state = 1

How would I achieve getting the CDATA text inside the tag (the first CDATA)

Upvotes: 2

Views: 3493

Answers (1)

Anand S Kumar
Anand S Kumar

Reputation: 90859

You can do -

ertag = child.find("error")
cdatatext = ertag.text
print(cdatatext)

This would print -

"HERES THE DETAILED ERROR"

Upvotes: 3

Related Questions