Parse XML with using ElementTree library
<p>I am trying to parse an xml data to print its content with ElementTree library in python 3. but when I print lst it returns me an empty list.</p>

<blockquote>
  <p>Error: Returning an empty array of list.</p>
</blockquote>

<p>My attempt:</p>

<pre><code>import xml.etree.ElementTree as ET
data = '''
    &lt;data&gt;
        &lt;country name="Liechtenstein"&gt;
            &lt;rank&gt;1&lt;/rank&gt;
            &lt;year&gt;2008&lt;/year&gt;
            &lt;gdppc&gt;141100&lt;/gdppc&gt;
            &lt;neighbor name="Austria" direction="E"/&gt;
            &lt;neighbor name="Switzerland" direction="W"/&gt;
        &lt;/country&gt;
        &lt;country name="Singapore"&gt;
            &lt;rank&gt;4&lt;/rank&gt;
            &lt;year&gt;2011&lt;/year&gt;
            &lt;gdppc&gt;59900&lt;/gdppc&gt;
            &lt;neighbor name="Malaysia" direction="N"/&gt;
        &lt;/country&gt;
    &lt;/data&gt;'''

tree = ET.fromstring(data)
lst = tree.findall('data/country')
print(lst)
#for item in lst:
#    print('Country: ', item.get('name'))
</code></pre>

<p>Thanks in advance.</p>

<p>Tree references the root item <code>&lt;data&gt;</code> already so it shouldn't be in your path statement. Just find "country".</p>

<pre><code>import xml.etree.ElementTree as ET
data = '''
    &lt;data&gt;
        &lt;country name="Liechtenstein"&gt;
            &lt;rank&gt;1&lt;/rank&gt;
            &lt;year&gt;2008&lt;/year&gt;
            &lt;gdppc&gt;141100&lt;/gdppc&gt;
            &lt;neighbor name="Austria" direction="E"/&gt;
            &lt;neighbor name="Switzerland" direction="W"/&gt;
        &lt;/country&gt;
        &lt;country name="Singapore"&gt;
            &lt;rank&gt;4&lt;/rank&gt;
            &lt;year&gt;2011&lt;/year&gt;
            &lt;gdppc&gt;59900&lt;/gdppc&gt;
            &lt;neighbor name="Malaysia" direction="N"/&gt;
        &lt;/country&gt;
    &lt;/data&gt;'''

tree = ET.fromstring(data)
lst = tree.findall('data/country')
print(lst)

# check position in tree
print(tree.tag)
print(tree.findall('country'))
</code></pre>

python xml python-3.x