2012-08-04 66 views
-5

我需要解析SoapServer的这个响应的XML响应:如何解析SOAP PHP

<?xml version="1.0" encoding="UTF-8"?><SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing"> 
<SOAP-ENV:Header> 
    <wsa:MessageID SOAP-ENV:mustUnderstand="0">uuid:5f7271f0-de19-11e1-8035-e656d1754971</wsa:MessageID> 
    <wsa:To SOAP-ENV:mustUnderstand="0">http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</wsa:To> 
</SOAP-ENV:Header> 
<SOAP-ENV:Body> 
    <ns1:wssigatewayResponse xmlns:ns1="urn:it-progress-operate:ws_operate"> 
    <ns1:result xsi:nil="true"/> 
    <ttOut xmlns="urn:it-progress-operate:ws_operate"> 
    <ttOutRow xmlns="urn:it-progress-operate:ws_operate"> 
    <ParPos xmlns="urn:it-progress-operate:ws_operate">0</ParPos> 
    <ParNam xmlns="urn:it-progress-operate:ws_operate">ContentType</ParNam> 
    <ParVal xmlns="urn:it-progress-operate:ws_operate">text/xml</ParVal> 
    </ttOutRow> 
    <ttOutRow xmlns="urn:it-progress-operate:ws_operate"> 
    <ParPos xmlns="urn:it-progress-operate:ws_operate">1</ParPos> 
    <ParNam xmlns="urn:it-progress-operate:ws_operate">Result</ParNam> 
    <ParVal xmlns="urn:it-progress-operate:ws_operate">200</ParVal> 
    </ttOutRow> 
    <ttOutRow xmlns="urn:it-progress-operate:ws_operate"> 
    <ParPos xmlns="urn:it-progress-operate:ws_operate">2</ParPos> 
    <ParNam xmlns="urn:it-progress-operate:ws_operate">XMLDocumentOut</ParNam> 
    <ParVal xmlns="urn:it-progress-operate:ws_operate">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?&gt; 
&lt;DtsAgencyLoginResponse xmlns=&quot;DTS&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xsi:schemaLocation=&quot;DTS file:///R:/xsd/DtsAgencyLoginMessage_01.xsd&quot;&gt;&lt;SessionInfo&gt;&lt;SessionID&gt;178918&lt;/SessionID&gt;&lt;Profile&gt;A&lt;/Profile&gt;&lt;Language&gt;ENG&lt;/Language&gt;&lt;Version&gt;1&lt;/Version&gt;&lt;/SessionInfo&gt;&lt;AdvisoryInfo/&gt;&lt;/DtsAgencyLoginResponse&gt;</ParVal> 
    </ttOutRow> 
    </ttOut> 
    <ns1:opcErrorMessage/> 
    </ns1:wssigatewayResponse> 
</SOAP-ENV:Body> 
</SOAP-ENV:Envelope> 

我怎么能在最后ttOutRow从ParVal得到的SessionID?

+2

Google“php parse xml” – 2012-08-04 09:59:09

+0

第一个看看网页右边的'Related'关于如何解析XML,然后解析XML,urldecode最后一个'ttOutRow'用simplexml_load_string加载解码后的字符串并解析再次找到会话ID。使用正则表达式来获取会话ID是很难的.. – Gntem 2012-08-04 10:04:49

+0

我尝试foreach($ xml-> ttOutRow作为$ ttout) { echo $ ttout ['ParVal']; }但它不会进入foreach ... – JackTurky 2012-08-04 10:05:50

回答

1

负载SOAP响应成DOMDocument对象:

$soapDoc = new DOMDocument(); 
$soapDoc->loadXML($soapResponse); 

准备DOMXPath对象该文档:

$xpath = new DOMXPath($soapDoc); 

注册为urn:it-progress-operate:ws_operate命名空间的前缀:

$xpath->registerNamespace('operate', 'urn:it-progress-operate:ws_operate'); 

检索有效负载节点:

$path = "//operate:ttOutRow[operate:ParNam='XMLDocumentOut']/operate:ParVal"; 
$result = $xpath->query($path); 

保存有效载荷XML:

$payloadXML = $result->item(0)->nodeValue; 

现在你已经有效载荷XML字符串,经历的过程再次:

  • 加载到一个DOMDocument
  • 准备DOMXpath对象
  • 注册DTS命名空间
  • 使用XPath检索值

这可能是最好的全过程包装成一个功能,以便您可以重新使用它。

+0

我从未见过的最佳问题!谢谢!!! :) – JackTurky 2012-08-04 10:22:06