【问题标题】:How to parse xml response of soap php [closed]如何解析soap php的xml响应[关闭]
【发布时间】:2012-08-02 04:54:55
【问题描述】:

我需要解析soapserver的这个响应:

<?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?

【问题讨论】:

  • 首先查看网页右侧的Related,了解如何解析该xml,然后解析该XML,对最后一个ttOutRow进行urldecode,使用simplexml_load_string加载解码后的字符串并再次解析为查找会话 ID。使用正则表达式来获取会话 ID 是很困难的..
  • 我试试 foreach($xml->ttOutRow as $ttout) { echo $ttout['ParVal']; } 但它没有进入 foreach...

标签: php soap response


【解决方案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 检索值

最好将整个过程封装到一个函数中,这样您就可以重复使用它。

【讨论】:

  • 我从未见过的最好的问题!谢谢!!! :)
猜你喜欢
  • 2016-09-03
  • 1970-01-01
  • 2011-09-21
  • 1970-01-01
  • 1970-01-01
  • 2021-06-07
  • 2012-09-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多