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

使用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 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,
  • ¥15 spaceclaim模型变灰色
  • ¥15 求一份华为esight平台V300R009C00SPC200这个型号的api接口文档
  • ¥15 字符串比较代码的漏洞
  • ¥15 欧拉系统opt目录空间使用100%
  • ¥15 ul做导航栏格式不对怎么改?