2012-07-25 133 views
1

我試圖解析來自服務器的SOAP響應。我是SOAP的全新新人,並且是使用HTTP/HTTPS進行溝通的新手。我在Ubuntu 12.04上使用Python 2.7。在Python中解析SOAP響應

看起來像SOAP非常像XML。但是,我似乎無法解析它。我試圖使用ElementTree,但不斷收到錯誤。從搜索中我可以得出結論,SOAP標籤可能存在問題。 (我可以在這裏...讓我知道如果我是。)

所以,這裏是一個SOAP消息的例子,我試圖做的解析它(這是一個實際的來自Link Point Gateway的服務器響應,以防相關)。

import xml.etree.ElementTree as ET 
soap_string = '<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Body><fdggwsapi:FDGGWSApiOrderResponse xmlns:fdggwsapi="http://secure.linkpt.net/fdggwsapi/schemas_us/fdggwsapi"><fdggwsapi:CommercialServiceProvider/><fdggwsapi:TransactionTime>Wed Jul 25 10:26:40 2012</fdggwsapi:TransactionTime><fdggwsapi:TransactionID/><fdggwsapi:ProcessorReferenceNumber/><fdggwsapi:ProcessorResponseMessage/><fdggwsapi:ErrorMessage>SGS-002303: Invalid credit card number.</fdggwsapi:ErrorMessage><fdggwsapi:OrderId>1</fdggwsapi:OrderId><fdggwsapi:ApprovalCode/><fdggwsapi:AVSResponse/><fdggwsapi:TDate/><fdggwsapi:TransactionResult>FAILED</fdggwsapi:TransactionResult><fdggwsapi:ProcessorResponseCode/><fdggwsapi:ProcessorApprovalCode/><fdggwsapi:CalculatedTax/><fdggwsapi:CalculatedShipping/><fdggwsapi:TransactionScore/><fdggwsapi:FraudAction/><fdggwsapi:AuthenticationResponseCode/></fdggwsapi:FDGGWSApiOrderResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>' 
targetTree = ET.fromstring(soap_string) 

我們得到以下錯誤:

unbound prefix: line 1, column 0 

從另一個計算器post我已經得出結論,SOAP-ENV:Body可能導致一個命名空間的問題。 (我可能是錯的。)

我已經做了其他搜索找到一個很好的解決方案,解析SOAP,但其中大多數是從3年以上。看來suds非常值得推薦。在我走得太遠之前,我想獲得「更新」的建議。

任何人都可以推薦一個固定的(和簡單的)方法來解析SOAP響應,就像我上面收到的響應一樣嗎?如果你能提供一個簡單的例子讓我開始(如上所述,我完全是SOAP的新手),這將不勝感激。

回答

0

我無法找到使用Python的直接方法。我決定改用PHP。

就像下面:

的Python:

import subprocess 
command = 'php /path/to/script.php "{1}"'.format(soap_string) 
process = subprocess.Popen(command, shell = True, stderr = subprocess.PIPE, stdout = subprocess.PIPE) 
process.wait() 
output = process.communicate()[0] 
(error, result, order_id) = output.split(',') 

PHP:

#!/usr/bin/php 
<?php 

$soap_response = $argv[1]; 

$doc = simplexml_load_string($soap_response); 
$doc->registerXPathNamespace('fdggwsapi', 'http://secure.linkpt.net/fdggwsapi/schemas_us/fdggwsapi'); 
$nodes = $doc->xpath('//fdggwsapi:FDGGWSApiOrderResponse/fdggwsapi:ErrorMessage'); 
$error = strval($nodes[0]); 

$nodes = $doc->xpath('//fdggwsapi:FDGGWSApiOrderResponse/fdggwsapi:TransactionResult'); 
$result = strval($nodes[0]); 

$nodes = $doc->xpath('//fdggwsapi:FDGGWSApiOrderResponse/fdggwsapi:OrderId'); 
$order_id = strval($nodes[0]); 

$array = array($error, $result, $order_id); 
$response = implode(',', $array); 

echo $response; 

此代碼僅解析這個特殊SOAP響應的具體方面。它應該足以讓你解決你的問題。

對於PHP來說,我是一個完整的新手(我已經使用Perl了一點,所以幫助)。我必須給予@scoffeyhis solution來解析SOAP的方式終於對我有意義。

0

編輯: 在Python中使用SOAP非常有趣 - 大多數工具不會維護多年。如果我們談論功能 - 也許ZSI是領導者。但是如果支持一些更復雜的XSD架構(只是一個例子 - 它不支持基於擴展的聯合和複雜類型,其中擴展類型不是基類型),它會有很多錯誤。 Suds非常易於使用,但並不像ZSI那麼強大 - 它比ZSI對一些複雜的XSD構造的支持更差。 有一個有趣的工具 - generateDS,它與XSD協同工作,而不是直接與WSDL協同工作 - 您必須自己實現這些方法。但它實際上做得很好。

+0

對不起,我沒有注意到,並忘記更新問題。 – Rico 2012-07-25 21:56:24

+0

編輯的問題和例子適用於我。我沒有得到這個錯誤...有什麼遺漏嗎? – Tisho 2012-07-25 21:58:17

+0

我不這麼認爲......我一定錯過了第一個信封。 = -/ – Rico 2012-07-25 22:06:44