2017-05-02 23 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'