如何解析soap php的xml响应 [英] How to parse xml response of soap php
本文介绍了如何解析soap php的xml响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要解析soapserver的这个响应:
i need to parse this response of 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"><?xml version="1.0" encoding="utf-8" ?>
<DtsAgencyLoginResponse xmlns="DTS" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="DTS file:///R:/xsd/DtsAgencyLoginMessage_01.xsd"><SessionInfo><SessionID>178918</SessionID><Profile>A</Profile><Language>ENG</Language><Version>1</Version></SessionInfo><AdvisoryInfo/></DtsAgencyLoginResponse></ParVal>
</ttOutRow>
</ttOut>
<ns1:opcErrorMessage/>
</ns1:wssigatewayResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
如何从最后一个 ttOutRow 中的 ParVal 获取 SessionID?
How can i get SessionID from ParVal in the last ttOutRow?
推荐答案
将 SOAP 响应加载到 DOMDocument
对象中:
Load the SOAP response into an DOMDocument
object:
$soapDoc = new DOMDocument();
$soapDoc->loadXML($soapResponse);
为该文档准备一个 DOMXPath
对象:
Prepare a DOMXPath
object for that document:
$xpath = new DOMXPath($soapDoc);
为 urn:it-progress-operate:ws_operate
命名空间注册前缀:
Register a prefix for the urn:it-progress-operate:ws_operate
namespace:
$xpath->registerNamespace('operate', 'urn:it-progress-operate:ws_operate');
检索有效载荷节点:
$path = "//operate:ttOutRow[operate:ParNam='XMLDocumentOut']/operate:ParVal";
$result = $xpath->query($path);
保存有效负载 XML:
Save the payload XML:
$payloadXML = $result->item(0)->nodeValue;
既然您有了有效负载 XML 字符串,请再次执行此过程:
Now that you have the payload XML string, go through the process again:
- 将其加载到 DOMDocument 中
- 准备一个 DOMXpath 对象
- 注册
DTS
命名空间 - 使用 XPath 检索值
最好将整个过程包装成一个函数,以便您可以重复使用它.
It's probably best to wrap the whole process into a function so you can re-use it.
这篇关于如何解析soap php的xml响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文