I'm trying to get return all UK artists in alphabetical order--I'm not fully understanding XML parsing. What I've gathered from debugging and the documentation is that when you use the findall() method it returns a list that cannot be further navigated, is this correct? So how do I iterate over the subelements of a parent node, in this case <cd>, to find all of the elements whose country=='UK'? Thanks in advance!
def get_uk_artists(xmlstr):
xml = ET.fromstring(xmlstr)
artist_list = []
for each in xml.findall('cd'):
if each.findall('./cd/country').text == 'UK':
artist_list.append(each.findall('artist').text)
return artist_list.sort()
The XML is:
xml_doc ='''<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
<cd>
<title>Empire Burlesque</title>
<artist sex="male">Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
<cd>
<title>Hide your heart</title>
<artist sex="female">Bonnie Tyler</artist>
<country>UK</country>
<company>CBS Records</company>
<price>9.90</price>
<year>1988</year>
</cd>
<cd>
<title>Greatest Hits</title>
<artist sex="female">Dolly Parton</artist>
<country>USA</country>
<company>RCA</company>
<price>9.90</price>
<year>1982</year>
</cd>
<cd>
<title>Still got the blues</title>
<artist sex="male">Gary Moore</artist>
<country>UK</country>
<company>Virgin records</company>
<price>10.20</price>
<year>1990</year>
</cd>
</catalog>
'''