Python Forum
Cant set api response as tree in Element tree
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Cant set api response as tree in Element tree
#1
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.

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)
Reply


Messages In This Thread
Cant set api response as tree in Element tree - by hey_arnold - Aug-03-2018, 09:43 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How and where to store a data for path tree? zayacEBN 1 1,968 Aug-21-2020, 10:14 PM
Last Post: Larz60+
  How to display XML tree structure with Python? sonicblind 13 42,324 Aug-12-2020, 02:05 PM
Last Post: mreshko
  Form add tree comments with mptt m0ntecr1st0 1 2,538 Feb-23-2019, 01:50 PM
Last Post: m0ntecr1st0
  [Tkinter] Does anybody know that how to edit tree view on tkinter Prince_Bhatia 11 14,373 Jul-31-2018, 01:37 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020