XML Parsing Child Elements using xml.etree.ElementTree
<p>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 <code>&lt;cd&gt;</code>, to find all of the elements whose country=='UK'? Thanks in advance! </p>

<pre><code>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()
</code></pre>

<p>The XML is: </p>

<pre><code> xml_doc ='''&lt;?xml version="1.0" encoding="ISO-8859-1"?&gt;
    &lt;catalog&gt;
        &lt;cd&gt;
            &lt;title&gt;Empire Burlesque&lt;/title&gt;
            &lt;artist sex="male"&gt;Bob Dylan&lt;/artist&gt;
            &lt;country&gt;USA&lt;/country&gt;
            &lt;company&gt;Columbia&lt;/company&gt;
            &lt;price&gt;10.90&lt;/price&gt;
            &lt;year&gt;1985&lt;/year&gt;
        &lt;/cd&gt;
        &lt;cd&gt;
            &lt;title&gt;Hide your heart&lt;/title&gt;
            &lt;artist sex="female"&gt;Bonnie Tyler&lt;/artist&gt;
            &lt;country&gt;UK&lt;/country&gt;
            &lt;company&gt;CBS Records&lt;/company&gt;
            &lt;price&gt;9.90&lt;/price&gt;
            &lt;year&gt;1988&lt;/year&gt;
        &lt;/cd&gt;
        &lt;cd&gt;
            &lt;title&gt;Greatest Hits&lt;/title&gt;
            &lt;artist sex="female"&gt;Dolly Parton&lt;/artist&gt;
            &lt;country&gt;USA&lt;/country&gt;
            &lt;company&gt;RCA&lt;/company&gt;
            &lt;price&gt;9.90&lt;/price&gt;
            &lt;year&gt;1982&lt;/year&gt;
        &lt;/cd&gt;
        &lt;cd&gt;
            &lt;title&gt;Still got the blues&lt;/title&gt;
            &lt;artist sex="male"&gt;Gary Moore&lt;/artist&gt;
            &lt;country&gt;UK&lt;/country&gt;
            &lt;company&gt;Virgin records&lt;/company&gt;
            &lt;price&gt;10.20&lt;/price&gt;
            &lt;year&gt;1990&lt;/year&gt;
        &lt;/cd&gt;
    &lt;/catalog&gt;
    '''
</code></pre>