正在尝试解析 Python 中的 XML 响应

Trying to parse XML response in Python

我正在尝试从此 XML 中获取 2 个元素的值(Result 和 AuthenticationKey)。 <s:Envelope> 语法让我失望。

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
   <s:Body>
      <AuthenticateResponse xmlns="http://remote.Services">
         <AuthenticateResult xmlns:a="http://schemas.datacontract.org/2004/07/remote.Services.Api.DataContracts" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
            <AdditionalInfo i:nil="true"/>
            <ErrorMessage i:nil="true"/>
            <ErrorMessageId i:nil="true"/>
            <ErrorMessages i:nil="true" xmlns:b="http://schemas.microsoft.com/2003/10/Serialization/Arrays"/>
            <InternalId>0</InternalId>
            <RecordsAffected>0</RecordsAffected>
            <Result>true</Result>
            <WarningMessages i:nil="true" xmlns:b="http://schemas.microsoft.com/2003/10/Serialization/Arrays"/>
            <a:AuthenticationKey>SUPERSECRETTOKEN</a:AuthenticationKey>
            <a:UserGrpId>YYY</a:UserGrpId>
            <a:UserId>XXX</a:UserId>
         </AuthenticateResult>
      </AuthenticateResponse>
   </s:Body>
</s:Envelope>

这是返回 None 的相关代码行。两行代码都返回 None.

我尝试使用 Whosebug 上另一个问题的代码,但我不确定如何在这个特定的 xml.

的 find 方法中引入名称空间
tree = ElementTree.fromstring(resp.text)
    token = tree.find("AuthenticateResponse") #Option1
    token = tree.find("Envelope/Body/AuthenticateResponse/AuthenticateResult/AuthenticationKey") #Option2
    print token

xmlns 部分成为 tags 的前缀,即要查找 AuthenticateResponse,您需要搜索 {http://remote.Services}AuthenticateResponse.

print(list(tree)[0].find('{http://remote.Services}AuthenticateResponse'))
>>> <Element '{http://remote.Services}AuthenticateResponse' at 0x00000000027228B8>

有关更通用的解决方案,请查看答案 here

from io import StringIO
tree = ET.iterparse(StringIO("""<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
   <s:Body>
      <AuthenticateResponse xmlns="http://remote.Services">
         <AuthenticateResult xmlns:a="http://schemas.datacontract.org/2004/07/remote.Services.Api.DataContracts" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
            <AdditionalInfo i:nil="true"/>
            <ErrorMessage i:nil="true"/>
            <ErrorMessageId i:nil="true"/>
            <ErrorMessages i:nil="true" xmlns:b="http://schemas.microsoft.com/2003/10/Serialization/Arrays"/>
            <InternalId>0</InternalId>
            <RecordsAffected>0</RecordsAffected>
            <Result>true</Result>
            <WarningMessages i:nil="true" xmlns:b="http://schemas.microsoft.com/2003/10/Serialization/Arrays"/>
            <a:AuthenticationKey>SUPERSECRETTOKEN</a:AuthenticationKey>
            <a:UserGrpId>YYY</a:UserGrpId>
            <a:UserId>XXX</a:UserId>
         </AuthenticateResult>
      </AuthenticateResponse>
   </s:Body>
</s:Envelope>"""))
for _, element in tree:
    element.tag = element.tag.split('}')[-1]

print(tree.root.find('Body').find('AuthenticateResponse').find('AuthenticateResult').find('AuthenticationKey').text)
>>> 'SUPERSECRETTOKEN'