Aug-03-2018, 09:43 PM
(This post was last modified: Aug-03-2018, 09:44 PM by hey_arnold.)
Hi All,
I can't get the response from the api to be recognised if I try and define tree. Any suggestions would be much appreciated.
I can't get the response from the api to be recognised if I try and define tree. Any suggestions would be much appreciated.
import requests import xml.etree.ElementTree as ET def getXML(): url="http://energywatch.natgrid.co.uk/EDP-PublicUI/PublicPI/InstantaneousFlowWebService.asmx" headers = {'content-type': 'application/soap+xml; charset=utf-8'} body ="""<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"> <soap12:Body> <GetInstantaneousFlowData xmlns="http://www.NationalGrid.com/EDP/UI/" /> </soap12:Body> </soap12:Envelope>""" response = requests.post(url,data=body,headers=headers) return response.content tree = ET.XML(response) root = tree.getroot() root.tag print(root.tag) for EDPObjectName in root.iter('EDPObjectName'): print(EDPObjectName.text)I can't work out what I'm doing wrong, because If I download the XML file then run the following code, it works:
import xml.etree.ElementTree as ET tree = ET.parse("C:\\Users\\Hey Arnold\\Documents\\Python Scripts\\new1.xml") root = tree.getroot() root.tag print(root.tag) for EDPObjectName in root.iter('EDPObjectName'): print(EDPObjectName.text)