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]);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?