Part of the XML file:
<?xml version="1.0" encoding="UTF-8"?>
<Station>
<Code>HT</Code>
<Type>knooppuntIntercitystation</Type>
<Namen>
<Kort>Den Bosch</Kort>
<Middel>'s-Hertogenbosch</Middel>
<Lang>'s-Hertogenbosch</Lang>
</Namen>
<Land>NL</Land>
<Synoniemen>
<Synoniem>Hertogenbosch ('s)</Synoniem>
<Synoniem>Den Bosch</Synoniem>
</Synoniemen>
</Station>
<Station>
<Code>ALMO</Code>
<Type>stoptreinstation</Type>
<Namen>
<Kort>Oostvaard</Kort>
<Middel>Oostvaarders</Middel>
<Lang>Almere Oostvaarders</Lang>
</Namen>
<Land>NL</Land>
<Synoniemen></Synoniemen>
</Station>
<Station>
<Code>ATN</Code>
<Type>stoptreinstation</Type>
<Namen>
<Kort>Aalten</Kort>
<Middel>Aalten</Middel>
<Lang>Aalten</Lang>
</Namen>
<Land>NL</Land>
<Synoniemen></Synoniemen>
</Station>
<Station>
<Code>ASA</Code>
<Type>intercitystation</Type>
<Namen>
<Kort>Amstel</Kort>
<Middel>Amsterdam Amstel</Middel>
<Lang>Amsterdam Amstel</Lang>
</Namen>
<Land>NL</Land>
<Synoniemen></Synoniemen>
</Station>
My python code to read xmlfile:
import xmltodict
def leesXML(filename):
with open(filename) as mijnXMLBestand:
inhoud = mijnXMLBestand.read()
xmldictionary = xmltodict.parse(inhoud)
return xmldictionary
stationsdictionary = leesXML('stations.xml')
stations = stationsdictionary['Stations']['Station']
def program():
for station in stations:
type = station['Type']
Code = station['Code']
print(Code + ' - ' + type)
print('Dit zijn de codes en types van de 4 stations:')
program()
With Program() I'll get a list with the type and code tag out of the xml file.
I'm trying now to figure out how to get the lang tag out of it and make a similair list like the one above but with only Code and Lang tags
I've made a new variable:
thirdtag = stationsdictionary['Stations']['Station']['Namen']
when I print this i'm getting an error
TypeError: list indices must be integers or slices, not str
Can anyone say what i'm doing wrong? Please use simple and readable python code. I'm a student started learning python.
Thanks