I am getting the XML response from the API call (use python). How can i parse the xml response ?
The xml response:
200 OK
<?xml version='1.0' encoding='UTF-8' ?>
<mail>
<delivery id='123'>
<deliver_id>xxx</deliver_id>
<request_id>xxx</request_id>
<exec_cnt>1</exec_cnt>
<result code='0'>success</result>
</delivery>
</mail>
Here is the main code in request to get response:
def sendMail():
context = ssl._create_unverified_context()
header = {"Content-Type": "text/xml"}
url = "xxx"
body = "xxx"
try:
conn = http.client.HTTPSConnection("54.199.162.74", context=context)
conn.request(method="POST", url=url, body=body.encode("UTF-8"), headers=header, encode_chunked=True)
response = conn.getresponse()
mail_response = response.read()
print(response.status, response.reason)
print(mail_response.decode('utf-8'))
except Exception as e:
print(e)
return response
sendMail()
I tried with this code to parse the xml response but always return error
import xml.etree.ElementTree as ET
response_xml_as_string = mail_response.decode('utf-8')
>>> tree = ET.fromstring(response_xml_as_string)
>>> root = tree.getroot()
>>> root.tag
'mail'
>>> root.attrib
{}
>>> for child in root:
... print(child.tag, child.attrib)
The error I got:
'xml.etree.ElementTree.Element' object has no attribute 'getroot'