2012-04-08 66 views
0

我想獲取父元素,我想到了一個XML標記。基本上,我需要通過多個<HotelRoomResponse>結果,並找到包含具有此確切數字值的子標記的父標記:<roomTypeCode>17918</roomTypeCode>我不知道如何做到這一點或什麼是最好的方法。因爲我然後需要獲取該特定的所有信息<HotelRoomResponse>。下面是一個例子XML響應:在xml和php中獲取父元素的索引?

<HotelRoomResponse> 
    <cancellationPolicy> </cancellationPolicy> 
    <rateCode>200482409</rateCode> 
    <roomTypeCode>17918</roomTypeCode> 
    <rateDescription> 
     Deluxe Sunset View - All Inclusive-Up to $300Resort Credit 
    </rateDescription> 
    <roomTypeDescription> 
     Deluxe Sunset View - All Inclusive-Up to $300Resort Credit 
    </roomTypeDescription> 
    <supplierType>E</supplierType> 
</HotelRoomResponse> 

所以有各種各樣的這些結果類型,我需要通過它循環,找到這個特定的一個。

這裏是我如何連接到XML:

$ch = curl_init(); 
$fp = fopen('room_request.xml','w'); 
curl_setopt($ch, CURLOPT_URL, "http://api.ean.com/ean-services/rs/hotel/v3/avail?cid=55505&minorRev=13&apiKey=4sr8d8bsn75tpcuja6ypx5g3&locale=en_US&currencyCode=USD&customerIpAddress=10.184.2.9&customerUserAgent=Mozilla/5.0+(Windows+NT+6.1)+AppleWebKit/535.11+(KHTML,+like+Gecko)+Chrome/17.0.963.79+Safari/535.11&customerSessionId=&xml=<HotelRoomAvailabilityRequest><hotelId>".$hid."</hotelId><arrivalDate>05/14/2012</arrivalDate><departureDate>05/18/2012</departureDate><RoomGroup><Room><numberOfAdults>3</numberOfAdults><numberOfChildren>0</numberOfChildren><childAges>0</childAges></Room></RoomGroup><includeDetails>true</includeDetails></HotelRoomAvailabilityRequest>"); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/xml')); 
curl_setopt($ch, CURLOPT_HEADER, 0); 
curl_setopt($ch, CURLOPT_FILE, $fp); 
$val = curl_exec($ch); 
curl_close($ch);//Close curl session 
fclose($fp); //Close file overwrite 
$avail = simplexml_load_file('room_request.xml'); 

任何想法都歡迎。

+0

您應該重新生成您的API密鑰。 – Till 2012-09-11 21:07:09

回答

0
$xml = new SimpleXMLElement('room_request.xml'); 

/* Search for <HotelRoomResponse><roomTypeCode> */ 
$result = $xml->xpath('/HotelRoomResponse/roomTypeCode'); 

結果會給你一個節點列表,那麼你可以檢查並獲取父節點,如果合適

看到這裏http://www.php.net/manual/en/simplexmlelement.xpath.php

編輯2. 關鍵是命名空間

<?php 
$ch = curl_init(); 
$fp = fopen('room_request.xml','w'); 
curl_setopt($ch, CURLOPT_URL, "http://travellinginmexico.com/test/room_request.xml"); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/xml')); 
curl_setopt($ch, CURLOPT_HEADER, 0); 
curl_setopt($ch, CURLOPT_FILE, $fp); 
$val = curl_exec($ch); 
curl_close($ch);//Close curl session 
fclose($fp); //Close file overwrite 

$xml = new SimpleXMLElement(file_get_contents('room_request.xml')); 

/* Search for <HotelRoomResponse><roomTypeCode> */ 
$xml->registerXPathNamespace('ns2', 'http://v3.hotel.wsapi.ean.com/'); 
$result = $xml->xpath("HotelRoomResponse[child::roomTypeCode[text() = '153725']]"); 
foreach($result as $obj=>$node) 
{ 
    var_dump($node->roomTypeCode); 
} 

隨着你發送的例子將獲得具體信息

+0

這沒有奏效,它給了我一個空數組? – liveandream 2012-04-08 15:45:50

+0

對不起變量名稱進行了編輯,我在HotelRoomResponse – 2012-04-08 15:58:06

+0

前面放了一個斜槓,仍然沒有任何內容。空數組。 – liveandream 2012-04-08 16:10:00

0

要找到具有與價值'17918'一個roomTypeCode子節點的所有節點HotelRoomResponse,使用以下命令:

$match = $avail->xpath("/HotelRoomResponse[child::roomTypeCode[text() = '17918']]"); 

編輯:$比賽將是一個數組保存所有的比賽。

+0

也沒有工作,給了我一個0的數組..你認爲你可以給我多一點例子?我試着把它放在foreach循環中,沒有任何東西。我不確定這裏發生了什麼。謝謝! – liveandream 2012-04-08 16:04:59

0

好吧,想出來了!這是我用來獲取text = value的節點。然後我得到了所有的兄弟元素。

// load as file 
$contents = new SimpleXMLElement($source,null,true); 
$result = $contents->xpath('HotelRoomResponse[roomTypeCode="17918"]'); 

foreach($result as $key=>$node) 
{ 
    $cancelPolicy = $node->cancellationPolicy; 
}