David Diep
David Diep

Reputation: 25

How to parse the xml response by python?

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'

Upvotes: 2

Views: 8666

Answers (2)

hungryspider
hungryspider

Reputation: 300

You can also use BeatuifulSoup to parse and extract content

from bs4 import BeautifulSoup as Soup
resp = """<?xml version='1.0' encoding='UTF-8' ?>
<mail>
 <delivery id='123'>
  <deliver_id>xxx</deliver_id>
  <request_id>yyy</request_id>
  <exec_cnt>1</exec_cnt>
  <result code='0'>success</result>
 </delivery>
</mail>"""

soup = Soup(resp,'xml')
#extracting data between request_id Tag
print(soup.request_id.get_text())

#extracting data between deliver ID Tag
print(soup.deliver_id.get_text())

#extracting the  result code and the data between the tag
print(soup.result['code'])
print(soup.result.get_text())

You can find more information about Beautiful soup in below link

https://www.crummy.com/software/BeautifulSoup/bs3/documentation.html

Upvotes: 4

C.Nivs
C.Nivs

Reputation: 13106

There is no getroot attribute for an ElementTree instance. You will start at the root of the tree, then you can iterate over the child elements using iter(tree):

import xml.ElementTree as ET

resp = """<?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>"""

tree = ET.fromstring(resp)

dir(tree)
# ['__class__', '__copy__', '__deepcopy__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__setstate__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'attrib', 'clear', 'extend', 'find', 'findall', 'findtext', 'get', 'getchildren', 'getiterator', 'insert', 'items', 'iter', 'iterfind', 'itertext', 'keys', 'makeelement', 'remove', 'set', 'tag', 'tail', 'text']

# No getroot method here

# just iterate over each element
for elem in tree:
    print(elem.text, elem.attrib)
    if elem.getchildren():
        # iterate over them too

Upvotes: 2

Related Questions