我正在从API调用中获取XML响应(使用python)。我如何解析xml响应?
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>
这是请求中获取响应的主要代码:
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()
我尝试使用此代码来解析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)
我得到的错误:
'xml.etree.ElementTree.Element'对象没有属性'getroot'
答案 0 :(得分:0)
getroot
实例没有ElementTree
属性。您将从树的根开始,然后可以使用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
答案 1 :(得分:0)
您还可以使用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())
您可以在下面的链接中找到有关美丽汤的更多信息
https://www.crummy.com/software/BeautifulSoup/bs3/documentation.html