I'm trying to parse this xml string using ElementTree in Python,
the data stored as a string,
xml = '''<?xml version="1.0" encoding="utf-8"?>
<SearchResults xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Student>
    <RollNumber>1</RollNumber>
    <Name>Abel</Name>
    <PhoneNumber>Not Included</PhoneNumber>
    <Email>[email protected]</Email>
    <Grade>7</Grade>
</Student>
<Student>
    <RollNumber>2</RollNumber>
    <Name>Joseph</Name>
    <PhoneNumber>Not Included</PhoneNumber>
    <Email>[email protected]</Email>
    <Grade>7</Grade>
</Student>
<Student>
    <RollNumber>3</RollNumber>
    <Name>Mike</Name>
    <PhoneNumber>Not Included</PhoneNumber>
    <Email>[email protected]</Email>
    <Grade>7</Grade>
</Student>
</SearchResults>'''
The code I used to parse this string as xml,
from xml.etree import ElementTree
xml = ElementTree.fromstring(xml)
results = xml.findall('Student')
for students in results:
    for student in students:
        print student.get('Name')
print results prints out the results as Elements,
[<Element 'Student' at 0x7feb615b4ad0>, <Element 'Student' at 0x7feb615b4c50>, <Element 'Student' at 0x7feb615b4e10>]
inside the for loop, print students  prints out the same,
<Element 'Student' at 0x7fd722d88ad0>
<Element 'Student' at 0x7fd722d88c50>
<Element 'Student' at 0x7fd722d88e10>
Anyway when I try to get the Name of the student using the print student.get('Name'), the program returns None.
What I'm trying to do is to pull the values from the xml for each tags and construct a dict.
You have a double loop here:
for students in results:
    for student in students:
        print student.get('Name')
students is one <Student> element. By iterating over that you get individual elements contained in that element. Those contained elements (<RollNumber>, <Name>, etc) have no Name attribute.
The .get() method only access attributes, but you appear to want to get the <Name> element. Use .find() or an XPath expression here instead:
for student in results:
    name = student.find('Name')
    if name is not None:
        print name.text
or
for student_name in xml.findall('.//Student/Name'):
    print name.text
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With