azarudeen ajees
azarudeen ajees

Reputation: 99

xpath query for find path in php

<HotelList>
  <HotelSummary>
    <hotelId>247331</hotelId>
    <name>The Accord</name>
    <RoomRateDetailsList>
      <RoomRateDetails>
        <roomTypeCode>200052939</roomTypeCode>
        <rateCode>200309117</rateCode>
        <RateInfos size="1">
          <RateInfo rateChange="false" promo="true" priceBreakdown="true">
            <RoomGroup>
              <Room>
                <numberOfAdults>2</numberOfAdults>
              </Room>
            </RoomGroup>
            <ChargeableRateInfo total="103.01" surchargeTotal="26.52" nightlyRateTotal="76.49" maxNightlyRate="76.49" currencyCode="USD" commissionableUsdTotal="76.49" averageRate="76.49" averageBaseRate="101.99">
          </ChargeableRateInfo>
        </RateInfo>
      </RateInfos>
    </RoomRateDetails>
  </RoomRateDetailsList>
</HotelSummary>
</HotelList>

Hello friends, I need to display values of <numberofresults> & <chargeablerateinfo> node in xpath or simpleXML plz give me how to access those elements. I used this code

  foreach($item ->('RoomRateDetailsList/RoomRateDetails/RateInfos/RateInfo /RoomGroup/Room/') as $items){
     echo $item->numberofresults;
  }

but it's not working plz give me suggestion or full coding thanks in advance

Upvotes: 0

Views: 326

Answers (2)

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243469

I need to display values of <numberofresults> & <chargeablerateinfo> node in xpath or simpleXML plz give me how to access those elements.

In the provided XML document there is no element named numberofresults -- you probably mean numberOfAdults.

This XPath expression:

//RateInfo/RoomGroup/Room

selects any Room element that is a child of a RoomGroup that is a child of any RateInfo element in the XML document.

This XPath expression:

//RateInfo/RoomGroup/Room/numberOfAdults

selects the numberOfAdults children of the elements selected by the previous expression.

And this XPath expression:

//RateInfo/ChargeableRateInfo 

selects the ChargeableRateInfo element associated with a RoomGroup

So, in your code you might want to do something like this:

$xml = simplexml_load_file("test.xml");

$rateInfos = $xml->xpath('//RateInfo');                
foreach ($rateInfos as $rateInfo) 
{    
   $rooms = $rateInfo->xpath('RoomGroup/Room');   

   foreach ($rooms as $room)
    {
      echo $room->numberOfAdults; 
    }

  $chargeableRateInfo = $rateInfo->xpath('ChargeableRateInfo');   

  // Do whatever is wanted with $chargeableRateInfo               
}  

Upvotes: 1

msgmash.com
msgmash.com

Reputation: 1035

$xml = simplexml_load_file("test.xml");
$rooms = $xml->xpath('//Room');
foreach ($rooms as $room) {
     echo $room->numberOfAdults;
}

should do the trick

Upvotes: 0

Related Questions