简体   繁体   English

如何通过python解析xml响应?

[英]How to parse the xml response by python?

I am getting the XML response from the API call (use python). 我从API调用(使用python)获取XML响应。 How can i parse the xml response ? 我如何解析xml响应?

The xml response: xml响应:

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 我尝试使用此代码来解析xml响应,但始终返回错误

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' 'xml.etree.ElementTree.Element'对象没有属性'getroot'

There is no getroot attribute for an ElementTree instance. ElementTree实例没有getroot属性。 You will start at the root of the tree, then you can iterate over the child elements using iter(tree) : 您将从树的根开始,然后可以使用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

You can also use BeatuifulSoup to parse and extract content 您还可以使用BeatuifulSoup解析和提取内容

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 https://www.crummy.com/software/BeautifulSoup/bs3/documentation.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM