2017-05-02 21 views
0

我试图从这个XML中获取2个元素的值(Result和AuthenticationKey)。 <s:Envelope>语法正在抛弃我。尝试解析Python中的XML响应

<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。

我试图使用从另一个问题上的代码在stackoverflow,但我不知道如何在这个特定的XML的find方法中引入名称空间。

tree = ElementTree.fromstring(resp.text) 
    token = tree.find("AuthenticateResponse") #Option1 
    token = tree.find("Envelope/Body/AuthenticateResponse/AuthenticateResult/AuthenticationKey") #Option2 
    print token 
+0

可能的重复http://stackoverflow.com/questions/14853243/parsing-xml-with-namespace-in-python-via-elementtree – Milo

回答

0

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'