Parsing XML in Python with multiple tags
<p>I'm trying to parse an XML file.<br>
I succeeded at parsing tags at the upper layer, but now I have a tag within a tag and I'm not getting the correct output.</p>

<p><strong>XML FILE:</strong></p>

<pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
    &lt;Stations&gt;
    &lt;Station&gt;
    &lt;Code&gt;HT&lt;/Code&gt;
    &lt;Type&gt;knooppuntIntercitystation&lt;/Type&gt;
    &lt;Namen&gt;
    &lt;Kort&gt;Den Bosch&lt;/Kort&gt;
    &lt;Middel&gt;'s-Hertogenbosch&lt;/Middel&gt;
    &lt;Lang&gt;'s-Hertogenbosch&lt;/Lang&gt;
    &lt;/Namen&gt;
    &lt;Land&gt;NL&lt;/Land&gt;
    &lt;Synoniemen&gt;
    &lt;Synoniem&gt;Hertogenbosch ('s)&lt;/Synoniem&gt;
    &lt;Synoniem&gt;Den Bosch&lt;/Synoniem&gt;
    &lt;/Synoniemen&gt;
    &lt;/Station&gt;
    &lt;Station&gt;
    &lt;Code&gt;ALMO&lt;/Code&gt;
    &lt;Type&gt;stoptreinstation&lt;/Type&gt;
    &lt;Namen&gt;
    &lt;Kort&gt;Oostvaard&lt;/Kort&gt;
    &lt;Middel&gt;Oostvaarders&lt;/Middel&gt;
    &lt;Lang&gt;Almere Oostvaarders&lt;/Lang&gt;
    &lt;/Namen&gt;
    &lt;Land&gt;NL&lt;/Land&gt;
    &lt;Synoniemen&gt;
    &lt;/Synoniemen&gt;
    &lt;/Station&gt;
    &lt;Station&gt;
    &lt;Code&gt;ATN&lt;/Code&gt;
    &lt;Type&gt;stoptreinstation&lt;/Type&gt;
    &lt;Namen&gt;
    &lt;Kort&gt;Aalten&lt;/Kort&gt;
    &lt;Middel&gt;Aalten&lt;/Middel&gt;
    &lt;Lang&gt;Aalten&lt;/Lang&gt;
    &lt;/Namen&gt;
    &lt;Land&gt;NL&lt;/Land&gt;
    &lt;Synoniemen&gt;
    &lt;/Synoniemen&gt;
    &lt;/Station&gt;
    &lt;Station&gt;
    &lt;Code&gt;ASA&lt;/Code&gt;
    &lt;Type&gt;intercitystation&lt;/Type&gt;
    &lt;Namen&gt;
    &lt;Kort&gt;Amstel&lt;/Kort&gt;
    &lt;Middel&gt;Amsterdam Amstel&lt;/Middel&gt;
    &lt;Lang&gt;Amsterdam Amstel&lt;/Lang&gt;
    &lt;/Namen&gt;
    &lt;Land&gt;NL&lt;/Land&gt;
    &lt;Synoniemen&gt;
    &lt;/Synoniemen&gt;
    &lt;/Station&gt;
    &lt;/Stations&gt;
</code></pre>

<p><strong>My python function:</strong></p>

<pre><code>import xml.etree.ElementTree

e = xml.etree.ElementTree.parse('info.xml').getroot()

for stationsnamens in e.findall('Station'):
    try:
        syn = stationsnamens.find('Synoniemen/Synoniem').text
        print(syn)
    except:
        print(Exception)
</code></pre>

<p>I'm trying to print every <code>Synoniemen</code> field there is, but only if it exists. Also, the 'Code' needs to be printed.  </p>

<p><strong>Output Format:</strong>  </p>

<pre><code>{Code}: {Synoniemen}
</code></pre>

<p>something like this (note: I have used <code>.fromstring()</code> in this example, but you can modify this for your own use with files)</p>

<pre><code>import xml.etree.ElementTree
xmlstring = "&lt;root&gt;&lt;synoniemen&gt;&lt;synoniem&gt;A&lt;/synoniem&gt;&lt;synoniem&gt;B&lt;/synoniem&gt;&lt;/synoniemen&gt;&lt;/root&gt;"
e = xml.etree.ElementTree.fromstring(xmlstring)
syn = e.find('synoniemen')
for synoniem in syn:
    print(synoniem.text)
</code></pre>

<p>point is that <code>syn</code> is a iterable  with a <code>for</code> as it contains multiple elements.</p>

<p>So your code will look something like this:</p>

<pre><code>for stationsnamens in e.findall('Station'):
    code = stationsnames.find('Code')
    try:
        syn = stationsnamens.find('Synoniemen')
        for synoniem in syn:
            print(code.text, synoniem.text)
    except:
        print(Exception)
</code></pre>

python python-3.x