2012-05-08 47 views
0

我是SoapUI和SoapUI Pro的新手。最近我遇到了一個財產轉移問題,其中我嘗試用Google搜索解決方案時沒有運氣。如何從肥皂響應中檢索節點?

我與「國家代碼」從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技能是限制我自己解決它的能力。非常感謝任何人都可以提供幫助。

回答

0

以下XPath表達式獲取完整響應是非法的。

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

因爲它的GetCountriesResult包含CDATA值並且它沒有被XML解析器解析。所以你需要得到結果並將其放入臨時流中,然後解析並獲取值。

+0

喜新,你對如何檢索從CDATA那麼節點什麼建議嗎? –

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; 
     }