如何parsingSOAP XML?
SOAP XML:
<?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <PaymentNotification xmlns="http://apilistener.envoyservices.com"> <payment> <uniqueReference>ESDEUR11039872</uniqueReference> <epacsReference>74348dc0-cbf0-df11-b725-001ec9e61285</epacsReference> <postingDate>2010-11-15T15:19:45</postingDate> <bankCurrency>EUR</bankCurrency> <bankAmount>1.00</bankAmount> <appliedCurrency>EUR</appliedCurrency> <appliedAmount>1.00</appliedAmount> <countryCode>ES</countryCode> <bankInformation>Sean Wood</bankInformation> <merchantReference>ESDEUR11039872</merchantReference> </payment> </PaymentNotification> </soap:Body> </soap:Envelope>
如何获得“付款”元素?
我尝试parsing(PHP)
$xml = simplexml_load_string($soap_response); $xml->registerXPathNamespace('soap', 'http://schemas.xmlsoap.org/soap/envelope/'); foreach ($xml->xpath('//payment') as $item) { print_r($item); }
结果是空的:(任何想法如何parsing它是正确的?
处理命名空间前缀最简单的方法之一就是简单地将它们从XML响应中去除,然后传递给simplexml,如下所示:
$your_xml_response = '<Your XML here>'; $clean_xml = str_ireplace(['SOAP-ENV:', 'SOAP:'], '', $your_xml_response); $xml = simplexml_load_string($clean_xml);
这将返回以下内容:
SimpleXMLElement Object ( [Body] => SimpleXMLElement Object ( [PaymentNotification] => SimpleXMLElement Object ( [payment] => SimpleXMLElement Object ( [uniqueReference] => ESDEUR11039872 [epacsReference] => 74348dc0-cbf0-df11-b725-001ec9e61285 [postingDate] => 2010-11-15T15:19:45 [bankCurrency] => EUR [bankAmount] => 1.00 [appliedCurrency] => EUR [appliedAmount] => 1.00 [countryCode] => ES [bankInformation] => Sean Wood [merchantReference] => ESDEUR11039872 ) ) ) )
PHP版本> 5.0有一个很好的SoapClient集成。 哪些不需要parsing响应xml。 这是一个简单的例子
$client = new SoapClient("http://path.to/wsdl?WSDL"); $res = $client->SoapFunction(array('param1'=>'value','param2'=>'value')); echo $res->PaymentNotification->payment;
在您的代码中,您正在查询缺省名称空间中的payment
元素,但是在XML响应中,它是在http://apilistener.envoyservices.com
名称空间中声明的。
所以, 你错过了一个名字空间的声明 :
$xml->registerXPathNamespace('envoy', 'http://apilistener.envoyservices.com');
现在,您可以在xpath查询中使用envoy
命名空间前缀:
xpath('//envoy:payment')
完整的代码将是:
$xml = simplexml_load_string($soap_response); $xml->registerXPathNamespace('envoy', 'http://apilistener.envoyservices.com'); foreach ($xml->xpath('//envoy:payment') as $item) { print_r($item); }
注意 :我删除了soap
命名空间声明,因为您似乎没有使用它(只有在您使用xpath查询中的命名空间前缀时才有用)。
$xml = '<?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <PaymentNotification xmlns="http://apilistener.envoyservices.com"> <payment> <uniqueReference>ESDEUR11039872</uniqueReference> <epacsReference>74348dc0-cbf0-df11-b725-001ec9e61285</epacsReference> <postingDate>2010-11-15T15:19:45</postingDate> <bankCurrency>EUR</bankCurrency> <bankAmount>1.00</bankAmount> <appliedCurrency>EUR</appliedCurrency> <appliedAmount>1.00</appliedAmount> <countryCode>ES</countryCode> <bankInformation>Sean Wood</bankInformation> <merchantReference>ESDEUR11039872</merchantReference> </payment> </PaymentNotification> </soap:Body> </soap:Envelope>'; $doc = new DOMDocument(); $doc->loadXML($xml); echo $doc->getElementsByTagName('postingDate')->item(0)->nodeValue; die;
结果是:
2010-11-15T15:19:45
为什么不尝试使用绝对的xPath
//soap:Envelope[1]/soap:Body[1]/PaymentNotification[1]/payment
或者因为你知道这是一个付款和付款没有任何属性,只需从付款中直接select
//soap:Envelope[1]/soap:Body[1]/PaymentNotification[1]/payment/*