【发布时间】:2019-02-23 10:36:57
【问题描述】:
我正在尝试使用 xml ElementTree 类解析我的 restapi api 响应。 我的xml是
<?xml version="1.0" encoding="utf-8"?>
<s:Envelope
xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header>
<h:ServerVersionInfo MajorVersion="15" MinorVersion="20" MajorBuildNumber="1643" MinorBuildNumber="20" Version="V2018_01_08"
xmlns:h="http://schemas.microsoft.com/exchange/services/2006/types"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
</s:Header>
<s:Body>
<m:FindFolderResponse
xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">
<m:ResponseMessages>
<m:FindFolderResponseMessage ResponseClass="Success">
<m:ResponseCode>NoError</m:ResponseCode>
<m:RootFolder IndexedPagingOffset="4" TotalItemsInView="4" IncludesLastItemInRange="true">
<t:Folders>
<t:Folder>
<t:FolderId Id="AQMkADAzNTg5ZjFmLWI2OTItNDFiOC1iNzg5LTNmOTJmOGUxYmUxNwAuAAADTM6dNOFmmUKiuKvh0ZUivQEAKpTTWhjp40e4J27GznKZBQACaFVC9AAAAA==" ChangeKey="AQAAABYAAAAqlNNaGOnjR7gnbsbOcpkFAAJov6Ax"/>
<t:ParentFolderId Id="AQMkADAzNTg5ZjFmLWI2OTItNDFiOC1iNzg5LTNmOTJmOGUxYmUxNwAuAAADTM6dNOFmmUKiuKvh0ZUivQEAKpTTWhjp40e4J27GznKZBQAAAgEMAAAA" ChangeKey="AQAAAA=="/>
<t:DisplayName>1</t:DisplayName>
<t:TotalCount>0</t:TotalCount>
<t:ChildFolderCount>0</t:ChildFolderCount>
<t:UnreadCount>0</t:UnreadCount>
</t:Folder>
<t:Folder>
<t:FolderId Id="AQMkADAzNTg5ZjFmLWI2OTItNDFiOC1iNzg5LTNmOTJmOGUxYmUxNwAuAAADTM6dNOFmmUKiuKvh0ZUivQEAKpTTWhjp40e4J27GznKZBQACaFVC9QAAAA==" ChangeKey="AQAAABYAAAAqlNNaGOnjR7gnbsbOcpkFAAJov6Az"/>
<t:ParentFolderId Id="AQMkADAzNTg5ZjFmLWI2OTItNDFiOC1iNzg5LTNmOTJmOGUxYmUxNwAuAAADTM6dNOFmmUKiuKvh0ZUivQEAKpTTWhjp40e4J27GznKZBQAAAgEMAAAA" ChangeKey="AQAAAA=="/>
<t:DisplayName>2</t:DisplayName>
<t:TotalCount>0</t:TotalCount>
<t:ChildFolderCount>0</t:ChildFolderCount>
<t:UnreadCount>0</t:UnreadCount>
</t:Folder>
<t:Folder>
<t:FolderId Id="AQMkADAzNTg5ZjFmLWI2OTItNDFiOC1iNzg5LTNmOTJmOGUxYmUxNwAuAAADTM6dNOFmmUKiuKvh0ZUivQEAKpTTWhjp40e4J27GznKZBQACaFVC9gAAAA==" ChangeKey="AQAAABYAAAAqlNNaGOnjR7gnbsbOcpkFAAJov6A1"/>
<t:ParentFolderId Id="AQMkADAzNTg5ZjFmLWI2OTItNDFiOC1iNzg5LTNmOTJmOGUxYmUxNwAuAAADTM6dNOFmmUKiuKvh0ZUivQEAKpTTWhjp40e4J27GznKZBQAAAgEMAAAA" ChangeKey="AQAAAA=="/>
<t:DisplayName>3</t:DisplayName>
<t:TotalCount>0</t:TotalCount>
<t:ChildFolderCount>0</t:ChildFolderCount>
<t:UnreadCount>0</t:UnreadCount>
</t:Folder>
<t:TasksFolder>
<t:FolderId Id="AQMkADAzNTg5ZjFmLWI2OTItNDFiOC1iNzg5LTNmOTJmOGUxYmUxNwAuAAADTM6dNOFmmUKiuKvh0ZUivQEAKpTTWhjp40e4J27GznKZBQAB+Vi2/gAAAA==" ChangeKey="BAAAABYAAAAqlNNaGOnjR7gnbsbOcpkFAAJov6A3"/>
<t:ParentFolderId Id="AQMkADAzNTg5ZjFmLWI2OTItNDFiOC1iNzg5LTNmOTJmOGUxYmUxNwAuAAADTM6dNOFmmUKiuKvh0ZUivQEAKpTTWhjp40e4J27GznKZBQAAAgEMAAAA" ChangeKey="AQAAAA=="/>
<t:DisplayName>Test_tasks</t:DisplayName>
<t:TotalCount>2</t:TotalCount>
<t:ChildFolderCount>0</t:ChildFolderCount>
<t:UnreadCount>0</t:UnreadCount>
</t:TasksFolder>
</t:Folders>
</m:RootFolder>
</m:FindFolderResponseMessage>
</m:ResponseMessages>
</m:FindFolderResponse>
</s:Body>
</s:Envelope>
在我的 xml 响应中,我有 3 个文件夹和 1 个任务文件夹。现在我想要 只为所有文件夹返回一个字典(id,changeKey) 项目。
这是我正在使用的代码
def __parse_createItem_response(xml_response):
response = {}
response_obj = et.XML(xml_response)
body = None
for e in response_obj:
if e.tag == '{http://schemas.xmlsoap.org/soap/envelope/}Body':
body = e
for e1 in body:
if e1.tag == '{http://schemas.microsoft.com/exchange/services/2006/messages}FindFolderResponse':
FindFolderResponse = e1
for e2 in FindFolderResponse:
if e2.tag == '{http://schemas.microsoft.com/exchange/services/2006/messages}ResponseMessages':
ResponseMessages = e2
for e3 in ResponseMessages:
if e3.tag == '{http://schemas.microsoft.com/exchange/services/2006/messages}FindFolderResponseMessage':
FindFolderResponseMessage = e3
if e3.attrib:
ResponseClass = e3.attrib['ResponseClass']
response['ResponseClass'] = ResponseClass
if 'Success' == ResponseClass:
for e4 in FindFolderResponseMessage:
if e4.tag == '{http://schemas.microsoft.com/exchange/services/2006/messages}RootFolder':
Rootfolder = e4
for e5 in Rootfolder:
if e5.tag == '{http://schemas.microsoft.com/exchange/services/2006/types}Folders':
Folders = e5
for p in Folders:
if p.tag == '{http://schemas.microsoft.com/exchange/services/2006/types}Folder':
folder = p
for fid in folder:
if fid.tag == '{http://schemas.microsoft.com/exchange/services/2006/types}FolderId':
response['FolderId'] = fid.attrib['Id']
if fid.attrib['ChangeKey']:
response['ChangeKey'] = fid.attrib[
'ChangeKey']
elif 'Error' == ResponseClass:
for e4 in FindFolderResponseMessage:
if e4.tag == '{http://schemas.microsoft.com/exchange/services/2006/messages}MessageText':
MessageText = e4.text
response['MessageText'] = MessageText
elif e4.tag == '{http://schemas.microsoft.com/exchange/services/2006/messages}ResponseCode':
ResponseCode = e4.text
response['ResponseCode'] = ResponseCode
return response
我得到的输出是 ->
{'FolderId': 'AQMkADAzNTg5ZjFmLWI2OTItNDFiOC1iNzg5LTNmOTJmOGUxYmUxNwAuAAADTM6dNOFmmUKiuKvh0ZUivQEAKpTTWhjp40e4J27GznKZBQACaFVC9gAAAA==', 'ChangeKey': 'AQAAABYAAAAqlNNaGOnjR7gnbsbOcpkFAAJov6A1', 'ResponseClass': '成功'}
它只返回具有 DispalyName 3 的文件夹。 我犯了什么错误。 任何帮助将不胜感激。
【问题讨论】:
标签: python xml xml-parsing elementtree