0
<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

8
  • display values for numberofresults and chargeableinfo Commented Mar 1, 2012 at 14:28
  • azarudeen ajees, Was my answer useful to you? If so, please, accept it (by clicking on the check mark next to the answer). Commented Mar 2, 2012 at 13:56
  • Your answer helped me but i am not find the check mark near it plz instruct Commented Mar 3, 2012 at 3:40
  • Visit this link: stackoverflow.com/faq#howtoask and expand the section (click on "show more" at the end of the "how do I ask..." section). Then the last picture of the expanded section shows how to accept an answer. Commented Mar 3, 2012 at 4:21
  • i clicked the tick mark check it mr.Dimitre Novatchev Commented Mar 3, 2012 at 4:28

2 Answers 2

1

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               
}  
Sign up to request clarification or add additional context in comments.

Comments

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

should do the trick

1 Comment

How to access attributes of ChargeableRateInfo in xpath php plz help it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.