Tillbaka till forum

Komma åt value i xml response

2019-04-15 11:58:31 av Exjobb2019
Hej, Genom mitt request så kan jag printa ut responsen för trafikinformation och får då nedan resultat. {{code}} <RESPONSE><RESULT><Situation><Deviation><Geometry><WGS84>POINT (16.109539 60.9519234)</WGS84></Geometry><IconId>frostDamage</IconId><MessageCode>Tjälskada</MessageCode><RoadNumber>Väg 877</RoadNumber><LocationDescriptor>Väg 877 i Falu kommun i Dalarnas län.</LocationDescriptor></Deviation><Deviation><Geometry><WGS84>POINT (16.109539 60.9519234)</WGS84></Geometry><IconId>trafficMessage</IconId><MessageCode>Viktbegränsning gäller</MessageCode><RoadNumber>Väg 877</RoadNumber></Deviation><PublicationTime>2019-03-13T19:45:25</PublicationTime></Situation></RESULT></RESPONSE> {{/code}} Tydligt så innehåller mitt respons värden för för de olika keys jag har (IconId, Geometry, RoadNumber etc). När jag dock gör en for-slinga för att sedan printa innehållet för varje key så visas value som tomma dictionaries "{}" eller som "None" när jag har använt .get("något key"). Se nedan. {{code}} ('RESULT', {}) ('Situation', {}) ('Deviation', {}) ('attributes2', 'Geometry', 'WGS84', None) ('coordtag', 'WGS84', 'None') ('attributes2', 'IconId', 'WGS84', None) ('attributes2', 'MessageCode', 'WGS84', None) ('attributes2', 'RoadNumber', 'WGS84', None) ('attributes2', 'LocationDescriptor', 'WGS84', None) ('Deviation', {}) ('attributes2', 'Geometry', 'WGS84', None) ('coordtag', 'WGS84', 'None') ('attributes2', 'IconId', 'WGS84', None) ('attributes2', 'MessageCode', 'WGS84', None) ('attributes2', 'RoadNumber', 'WGS84', None) ('PublicationTime', {}) {{/code}} Är det någon som vet varför jag inte får ut något värde? Använder jag fel key eller är strukturen på responsen fel så jag inte kan komma åt något värde? Uppskattar all hjälp! Här är min kod: {{code}} import json import requests import xml.etree.ElementTree as ET data = """ <REQUEST> <LOGIN authenticationkey="MyAPIkey" /> <QUERY objecttype="Situation" limit = '1'> <FILTER> <OR> <ELEMENTMATCH> <EQ name="Deviation.MessageType" value="Hinder" /> </ELEMENTMATCH> <ELEMENTMATCH> <EQ name="Deviation.MessageType" value="Restriktion" /> </ELEMENTMATCH> <ELEMENTMATCH> <EQ name="Deviation.MessageType" value="Viktig trafikinformation" /> </ELEMENTMATCH> <ELEMENTMATCH> <EQ name="Deviation.MessageType" value="Olycka" /> </ELEMENTMATCH> <ELEMENTMATCH> <EQ name="Deviation.MessageType" value="Trafikmeddelande" /> </ELEMENTMATCH> </OR> </FILTER> <INCLUDE>PublicationTime</INCLUDE> <INCLUDE>Deviation.CountyNo[]</INCLUDE> <INCLUDE>Deviation.IconId</INCLUDE> <INCLUDE>Deviation.Geometry.WGS84</INCLUDE> <INCLUDE>Deviation.LocationDescriptor</INCLUDE> <INCLUDE>Deviation.Message</INCLUDE> <INCLUDE>Deviation.SeverityText</INCLUDE> <INCLUDE>Deviation.RoadNumber</INCLUDE> <INCLUDE>Deviation.MessageCode</INCLUDE> <INCLUDE>Deviation.NumberOfLanesRestricted</INCLUDE> </QUERY> </REQUEST>""" data2 = data.decode('utf-8') req = requests.post(url='http://api.trafikinfo.trafikverket.se/v1.3/data.xml', data=data2,headers = {'Content-Type':'text/xml'}) print(req.content) root = ET.fromstring(req.content) for RESULT in root: print(RESULT.tag, RESULT.attrib) for Situation in RESULT: print(Situation.tag, Situation.attrib) for attributes1 in Situation: print (attributes1.tag, attributes1.attrib) for attributes2 in attributes1: print ('attributes2', attributes2.tag, 'WGS84', attributes2.attrib.get('WGS84')) for coord in attributes2: print('coordtag', coord.tag, str(coord.attrib.get('WGS84'))) {{/code}}
Svara