I am trying to write a Python script that prints the value of a particular tag in an XML output. Here, the tag value I need to print is the value of in each and every occurrences in the XML output. I tried as below, but it shows an attribute error. What could be wrong here? Which is the correct way of getting and print values of certain more tags which I am interested to see? Any help please? Thanks.
import xml.etree.ElementTree as ET
mystring="""<?xml version="1.0" encoding="UTF-8"?>
<main>
<student>
<male>
<result>pass</result>
<name>Paul</name>
<address>boston</address>
<localreference>
<name>Charlie</name>
</localreference>
</male>
<female>
<result>pass</result>
<name>Rose</name>
<address>newyork</address>
<localreference>
<name>Charlie</name>
</localreference>
</female>
</student>
<student>
<male>
<result>fail</result>
<name>Philippe</name>
<address>boston</address>
<localreference>
<name>White</name>
</localreference>
</male>
</student>
</main>"""
main = ET.fromstring(mystring)
for student in main:
if (student.tag == "student"):
print student.find("male/result").text
print student.find("female/result").text
Error>
# python new5.py
pass
pass
fail
Traceback (most recent call last):
File "new5.py", line 39, in <module>
print student.find("female/result").text
AttributeError: 'NoneType' object has no attribute 'text'