2012-08-04 90 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