获取包含xml中具有特定字符串的子元素的所有根元素


Getting all root elements that contain child element with certain string in xml

嗨,我正试图从xml中只返回某个元素。。这样解释更容易:

<HotelList activePropertyCount="129" size="20">
<HotelSummary ubsScore="820" order="0">
<hotelId>121196</hotelId>
<name>ME Cancun - Complete ME All Inclusive</name>
<address1>Boulevard Kukulkan Km 12</address1>
<city>Cancun</city>
<postalCode>77500</postalCode>
<countryCode>MX</countryCode>
</HotelSummary>
</HotelList>

这些都有不同的酒店ID。我需要得到具有<hotelID>121196</hotelID>的子元素的<HotelSummary>。我不知道该怎么做。我正在使用php,这是我迄今为止所拥有的全部:

<?
$post_string = 'type=xml&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=<HotelListRequest><arrivalDate>04/05/2012</arrivalDate><departureDate>04/07/2012</departureDate><RoomGroup><Room><numberOfAdults>2</numberOfAdults></Room><Room><numberOfAdults>2</numberOfAdults><numberOfChildren></numberOfChildren></Room></RoomGroup><city>Cancun</city><countryCode>MX</countryCode><supplierCacheTolerance>MED_ENHANCED</supplierCacheTolerance></HotelListRequest> ';
$path = "http://api.ean.com/ean-services/rs/hotel/v3/list"; //Relative path to the file with $_POST parsing
$ch = curl_init($path); 
$fp = fopen('room.xml','w');
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string); //Send the data to the file
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
$data = simplexml_load_file('room.xml');
$info=$data->HotelList->HotelSummary->hotelId="123658";
$rating=$info->hotelRating;
echo $rating;
?>

提前感谢!!!

循环并查找特定的id。

foreach($data->HotelList->HotelSummary as $hotel) {
    if ($hotel->hotelId == 121196) {
        // This is it.
        break;
    }
}