dqsp60748 2013-08-27 22:04
浏览 81
已采纳

使用PHP / xpath解析SOAP响应 - 不能超过一个节点

I've tried to read other tutorials & SO responses on this but I just can't seem to make it work :/

I'm able to make the SOAP request and get a response but I can't seem to parse the response.

$result = $client->GetAllAttributes($params);

And the resulting response xml is:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
 <soap:Body>
  <GetAllAttributesResponse xmlns="http://connect2.askadmissions.net/webservices/">
    <GetAllAttributesResult>
     <result>
      <code>1</code>
       <ErrorMessage />
       <returndata>
         <attributes>
           <attribute>
            <type>attribute</type>
            <level />
            <name>text1321</name>
            <mappingname><![CDATA[Extra-Curricular Interest]]></mappingname>
            <datatype>Varchar2</datatype>
            <size>35</size>
            <validationexp />
           </attribute>
           <attribute> (same as above, several of these are returned</attribute>
         </attributes>
       </returndata>
      </result>
     </GetAllAttributesResult>
    </GetAllAttributesResponse>
   </soap:Body>
  </soap:Envelope>
 </xml>

I've tried

$xml = simplexml_load_string($client->__getLastResponse());
print_r($xml);

But it just prints "SimpleXMLElement Object ( ) "

I've tried

$responseXML = $client->__getLastResponse();
        $xml = simplexml_load_string($responseXML);
        $xml->registerXPathNamespace('soap', 'http://schemas.xmlsoap.org/soap/envelope/');
        $xml->registerXPathNamespace('hob', 'http://connect2.askadmissions.net/webservices/');
        $item = $xml->xpath('//hob:GetAllAttributesResult');
        print_r($item);

and I get an array

Array
(
[0] => SimpleXMLElement Object
    (
        [0] => <result><code>1</code><ErrorMessage /><returndata><attributes><attribute>    <type>attribute</type><level />

etc. (array is very long)

My problem comes when I try to step further into the tree. If I do

$item = $xml->xpath('//hob:GetAllAttributesResult/hob:result');

or

$item = $xml->xpath('//hob:GetAllAttributesResult/hob:code');

I end up with an empty array.

How do I step further into the tree?

Thank you very much for any help.

  • 写回答

1条回答 默认 最新

  • duan111112 2013-08-27 22:43
    关注

    To access the element's value, you need to access the first element of the array. And you may need to cast it to string to get the string value e.g. in your example you can do:

    $item = $xml->xpath('//hob:GetAllAttributesResult/hob:result/hob:code');
    print_r((string)$item[0]);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 虚心请教几个问题,小生先有礼了
  • ¥30 截图中的mathematics程序转换成matlab