在 Python 中解析 SOAP 响应 [英] Parsing a SOAP response in Python
问题描述
我正在尝试解析来自服务器的 SOAP
响应.我是 SOAP
的 100% 新手,并且对使用 HTTP
/HTTPS
进行通信非常陌生.我在 Ubuntu 12.04 上使用 Python 2.7.
I'm trying to parse a SOAP
response from a server. I'm 100% new to SOAP
and pretty new to communicating using HTTP
/HTTPS
. I'm using Python 2.7 on Ubuntu 12.04.
看起来SOAP
与XML
非常相似.但是,我似乎无法这样解析它.我曾尝试使用 ElementTree
但不断出现错误.通过搜索,我得出结论,SOAP
标记可能存在问题.(我可能会离开这里......如果我在,请告诉我.)
It looks like SOAP
is very much like XML
. However, I seem to be unable to parse it as such. I've tried to use ElementTree
but keep getting errors. From searches I've been able to conclude that there may be issues with the SOAP
tags. (I could be way off here...let me know if I am.)
所以,这是我拥有的 SOAP
消息示例以及我尝试解析它的操作(这是来自 Link Point Gateway 的实际服务器响应,以防万一).
So, here is an example of the SOAP
message I have and what I'm trying to do to parse it (this is an actual server response from Link Point Gateway, in case that's relevant).
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
来自另一个 stackoverflow 帖子 我已经得出结论,SOAP-ENV:Body
可能会导致命名空间问题.(我可能是错的.)
From another stackoverflow post I've concluded that SOAP-ENV:Body
may be causing a namespace problem. (I could be wrong.)
我进行了其他搜索,以找到解析 SOAP
的好解决方案,但其中大部分来自 3 年多以前.似乎强烈推荐 suds.我想在走得太远之前获得更新"的建议.
I've done other searches to find a good solution for parsing SOAP
but most of them are from 3+ years ago. It seems that suds is pretty highly recommended. I wanted to get "updated" recommendations before I got too far down a path.
谁能推荐一种可靠(且简单)的方法来解析我上面收到的 SOAP
响应?如果您能提供一个让我入门的简单示例(正如我上面所说,我对 SOAP
完全陌生),我们将不胜感激.
Can anyone recommend a solid (and easy) way to parse a SOAP
response like the one I received above? It would be appreciated if you could provide a simple example to get me started (as I said above, I'm completely new to SOAP
).
推荐答案
我无法找到使用 Python 的直接方法.我决定改用 PHP.
I was unable to find a straight-forward approach using Python. I decided to use PHP instead.
很像以下内容:
蟒蛇:
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
响应的特定方面.这应该足以让您解决问题.
This code only parses specific aspects of this particular SOAP
response. It should be enough to get you going to solve your problem.
当谈到 PHP 时,我是一个完全的新手(我已经使用过一些 Perl,所以这有帮助).我必须感谢 @scoffey 对于 他的解决方案以最终的方式解析SOAP
对我来说很有意义.
I'm a complete newbie when it comes to PHP (I've used Perl a bit so that helped). I must give credit to @scoffey for his solution to parsing SOAP
in a way that finally made sense to me.
这篇关于在 Python 中解析 SOAP 响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!