0

こんにちは、特定のコンテンツまたは値を持つすべてのノードの数を取得しようとしています。この場合、私は次のようなxmlを持っています:

<hotel>
<city>Cancun</city>
<postalCode>77500</postalCode>
<countryCode>MX</countryCode>
<airportCode>CUN</airportCode>
<supplierType>E</supplierType>
<propertyCategory>1</propertyCategory>
<hotelRating>4.0</hotelRating>
<confidenceRating>52</confidenceRating>
</hotel>

値が 4 のすべての hotelRating を検索し、xml リストにある数をカウントする必要があります。PHPでこれを行う方法がわかりません。誰かがこれで私を助けることができれば、私はとても感謝しています!!

ちなみに既に以下のようにxmlファイルを接続しており、データの取得、送信はできています。

$post_string= 'type=xml&cid=222&minorRev=14&apiKey=222&locale='.$userLocale.'&currencyCode='.$userCurr.'&customerIpAddress='.$userIp.'&customerUserAgent='.$userAgent.'&xml=<HotelListRequest><destinationId>'.$destinationId.'</destinationId><arrivalDate>'.$arriving.'</arrivalDate><departureDate>'.$departing.'</departureDate><RoomGroup>'.$xmlQuery.'</RoomGroup><supplierCacheTolerance>MED_ENHANCED</supplierCacheTolerance></HotelListRequest> ';
//Relative path to the file with $_POST parsing
$path = "http://api.ean.com/ean-services/rs/hotel/v3/list"; 
$ch = curl_init($path); 
$fp = fopen('xml/data.xml','w');
//Send the data to the file
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string); 
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('xml/data.xml');
}

助けてくれてありがとう!!!

4

2 に答える 2

0
$count = $data->xpath("count(\"//hotel[hotelRating= '4.0']\")");

hotelrating = 4.0 のホテルの数が表示されます。

于 2012-09-12T01:05:15.037 に答える
0
foreach ($data->hotel as $hotel)
{
    if ($hotel->hotelRating == '4.0')
    {
        //  hotel with 4.0 rating found
    }
}
于 2012-09-12T00:55:14.353 に答える