如何从soap响应中检索节点?

时间:2012-05-08 02:53:08

标签: xml soap soapui

我是SoapUI和SoapUI Pro的新手。我最近遇到了一个财产转移问题,我在搜索解决方案时试图没有运气。

我正在使用webservicex.net上的“国家/地区代码”进行练习,当我运行“”GetCountry“测试请求时,我会得到一个国家/地区列表作为回复。

<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>
      <GetCountriesResponse xmlns="http://www.webserviceX.NET">
         <GetCountriesResult><![CDATA[<NewDataSet>
  <Table>
    <Name>Afghanistan, Islamic State of</Name>
  </Table>
  <Table>
    <Name>Albania</Name>
  </Table>
  <Table>
    <Name>Algeria</Name>
  </Table>
  .....
</NewDataSet>]]></GetCountriesResult>
      </GetCountriesResponse>
   </soap:Body>
</soap:Envelope>

这一切都很好,直到我只想从数据集中找回一个国家,例如阿尔及利亚。这是因为我想将国家/地区名称转移到下一个测试步骤,该步骤是将国家/地区名称作为请求的服务。我试图从响应中选择节点,但是我注意到我得到的XPath指向整个响应而不是其中一个节点

declare namespace ns1='http://www.webserviceX.NET';
//ns1:GetCountriesResponse[1]/ns1:GetCountriesResult[1] 

我想这对你们中的一些人来说可能是非常简单的问题,但我的XPath技能限制了我自己解决问题的能力。如果有人能提供帮助,我们非常感激。

2 个答案:

答案 0 :(得分:0)

在XPath表达式之后获得完整响应。

declare namespace ns1='http://www.webserviceX.NET';
//ns1:GetCountriesResponse[1]/ns1:GetCountriesResult[1] 

因为它的GetCountriesResult保存了CDATA值,并且它没有被XML解析器解析。因此,您需要获取结果并将其放入临时流中,然后解析并获取值。

答案 1 :(得分:0)

将您的命名空间名称放在此处

$sxe = new SimpleXMLElement($response);
            $sxe->registerXPathNamespace('d', 'urn:schemas-microsoft-com:xml-diffgram-v1');
            $result = $sxe->xpath("//NewDataSet");
            echo "<pre>";
            //print_r($result[0]);
            foreach ($result[0] as $title) {
                print_r($title);
                //echo $title->CityID;
        }
相关问题