在Python中解析SOAP响应

时间:2012-07-25 15:31:02

标签: python xml soap

我正在尝试从服务器解析SOAP响应。我是SOAP的新手,对使用HTTP / HTTPS进行沟通非常陌生。我在Ubuntu 12.04上使用Python 2.7。

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

从另一个stackoverflow post我得出结论SOAP-ENV:Body可能导致命名空间问题。 (我可能错了。)

我已经完成了其他搜索以找到解析SOAP的好解决方案,但其中大多数都来自3年多以前。似乎强烈推荐使用suds。我想在我走得太远之前得到“更新”的建议。

任何人都可以推荐一种可靠(简单)方法来解析SOAP响应,就像上面收到的那样吗?如果你能提供一个简单的例子让我入门(如上所述,我对SOAP完全不熟悉),将不胜感激。

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

EDITED: 在Python中使用SOAP非常有趣 - 大多数工具都不会维护多年。如果我们谈论功能 - 也许ZSI是领导者。但是如果它支持一些更复杂的XSD架构(仅仅是一个例子 - 它不支持基于扩展的联合和复杂类型,其中扩展类型不是基本类型),它有很多错误。 Suds非常容易使用,但不如ZSI那么强大 - 它对一些复杂的XSD结构的支持比ZSI更差。 有一个有趣的工具 - generateDS,它可以与XSD一起使用,而不是直接使用WSDL - 您必须自己实现这些方法。但它确实做得很好。