2017-06-14 204 views
0

我想使用python解析下面的xml。我不明白這是什麼類型的XML,因爲我從來沒有在這種類型的XML中工作過。我剛剛從微軟的api響應表單中獲得了它。解析Python中的soap/XML響應

現在我的問題是如何在我的python代碼中解析並獲取BinarySecurityToken的值。

我提到這個問題Parse XML SOAP response with Python

但是像這樣有也有一些的xmlns來獲取文本。但是在我的xml我看不到任何的xmlns附近值透過我能得到的價值。

請讓我知道如何從下面的XML使用python獲得特定字段的值。

<?xml version="1.0" encoding="utf-8" ?> 
<S:Envelope xmlns:S="http://www.w3.org/2003/05/soap-envelope" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsa="http://www.w3.org/2005/08/addressing"> 
    <S:Header> 
    <wsa:Action xmlns:S="http://www.w3.org/2003/05/soap-envelope" xmlns:wsa="http://www.w3.org/2005/08/addressing" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" wsu:Id="Action" S:mustUnderstand="1">http://schemas.xmlsoap.org/ws/2005/02/trust/RSTR/Issue</wsa:Action> 
    <wsa:To xmlns:S="http://www.w3.org/2003/05/soap-envelope" xmlns:wsa="http://www.w3.org/2005/08/addressing" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" wsu:Id="To" S:mustUnderstand="1">http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</wsa:To> 
    <wsse:Security S:mustUnderstand="1"> 
     <wsu:Timestamp xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" wsu:Id="TS"> 
     <wsu:Created>2017-06-12T10:23:01Z</wsu:Created> 
     <wsu:Expires>2017-06-12T10:28:01Z</wsu:Expires> 
     </wsu:Timestamp> 
    </wsse:Security> 
    </S:Header> 
    <S:Body> 
    <wst:RequestSecurityTokenResponse xmlns:S="http://www.w3.org/2003/05/soap-envelope" xmlns:wst="http://schemas.xmlsoap.org/ws/2005/02/trust" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:saml="urn:oasis:names:tc:SAML:1.0:assertion" xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:psf="http://schemas.microsoft.com/Passport/SoapServices/SOAPFault"> 
     <wst:TokenType>urn:passport:compact</wst:TokenType> 
     <wsp:AppliesTo xmlns:wsa="http://www.w3.org/2005/08/addressing"> 
     <wsa:EndpointReference> 
      <wsa:Address>https://something.something.something.com</wsa:Address> 
     </wsa:EndpointReference> 
     </wsp:AppliesTo> 
     <wst:Lifetime> 
     <wsu:Created>2017-06-12T10:23:01Z</wsu:Created> 
     <wsu:Expires>2017-06-13T10:23:01Z</wsu:Expires> 
     </wst:Lifetime> 
     <wst:RequestedSecurityToken> 
     <wsse:BinarySecurityToken Id="Compact0">my token</wsse:BinarySecurityToken> 
     </wst:RequestedSecurityToken> 
     <wst:RequestedAttachedReference> 
     <wsse:SecurityTokenReference> 
      <wsse:Reference URI="wwwww="> 
      </wsse:Reference> 
     </wsse:SecurityTokenReference> 
     </wst:RequestedAttachedReference> 
     <wst:RequestedUnattachedReference> 
     <wsse:SecurityTokenReference> 
      <wsse:Reference URI="swsw="> 
      </wsse:Reference> 
     </wsse:SecurityTokenReference> 
     </wst:RequestedUnattachedReference> 
    </wst:RequestSecurityTokenResponse> 
    </S:Body> 
</S:Envelope> 

回答

1

此聲明是根元素的開始標籤的一部分:

xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" 

這意味着與wsse前綴(如BinarySecurityToken)元素都在http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd命名空間。

該解決方案與鏈接問題的答案基本相同。這只是一個命名空間:

import xml.etree.ElementTree as ET 

tree = ET.parse('soap.xml')  
print tree.find('.//{http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd}BinarySecurityToken').text 

這裏是做的另一種方式:

import xml.etree.ElementTree as ET 

ns = {"wsse": "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"} 
tree = ET.parse('soap.xml') 
print tree.find('.//wsse:BinarySecurityToken', ns).text 

輸出在這兩種情況下是my token

請參閱https://docs.python.org/2.7/library/xml.etree.elementtree.html#parsing-xml-with-namespaces

+0

嘿認爲讓我試試這個然後我會接受 – burning

+0

感謝它現在的工作 – burning