Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/255.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 获取包含xml中具有特定字符串的子元素的所有根元素_Php_Xml - Fatal编程技术网

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

Php 获取包含xml中具有特定字符串的子元素的所有根元素,php,xml,Php,Xml,嗨,我正试图从xml中只返回某个元素。。这样更容易解释: <HotelList activePropertyCount="129" size="20"> <HotelSummary ubsScore="820" order="0"> <hotelId>121196</hotelId> <name>ME Cancun - Complete ME All Inclusive</name> <address1>Boul

嗨,我正试图从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>

121196
我坎昆-完整的我
库库坎大道12公里
坎昆
77500
MX
这里有很多不同的旅馆。我需要获取子元素为
121196
。我想不出怎么做。我正在使用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;
    }
}

这几乎奏效了!但你肯定回答了这个问题。我只是用$hotel换了$this,效果很好!非常感谢你@liveandream哎呀,我在JavaScript领域待得太久了。很高兴你成功了!