1

I am using curl in my project. When i am sending request, the response is coming in the form of XML as bellow. How can i convert this xml into array to present in the web page.

 <?xml version="1.0" encoding="UTF-8"?>
    <OTA_HotelAvailRS Version="1.0">
      <Success Id="140948"/>
       <Properties>
         <Property HotelCityCode="ELS" HotelCode="18918" HotelName="Premier Hotel Regent - Demo">
           <RelativePosition Direction="" Distance="0" DistanceUnitName=""/>
           <EssentialInfo>
           </EssentialInfo>
           <RoomStays>
                <RoomStay>
                     <RatePlans>
                          <RatePlan RatePlanCode="50010"/>
                     </RatePlans>
                     <RoomRates>
                          <RoomRate>
                               <Rates>
                                    <Rate EffectiveDate="2011-10-14" ExpireDate="2011-10-15">
                                         <Base Amount="114.00" CurrencyCode="EUR"/>

                                         <RateDescription Adults="1" Availability="A" Children="0" RoomNum="1">
                                              Standard
                                         </RateDescription>
                                    </Rate>
                               </Rates>
                          </RoomRate>
                     </RoomRates>
                     <Meals Description="Breakfast Buffet" MealType="Breakfast"/>
                </RoomStay>
           </RoomStays>
           <Promotions/>
           <AdditionalInfo>
                <HotelStarDetail rating="3"/>
                <HotelImages>
                     <HotelImage Type="" URL="http://image1.urlforimages.com/1204258guest.jpg"/>
                </HotelImages>
                <HotelDescription>
                     <LongDescription> description</LongDescription>
                </HotelDescription>
           </AdditionalInfo>
      </Property>          
 </Properties>

0

1 Answer 1

6

Why convert it to an array at all? PHP is pretty good with XML. Just load up the XML in SimpleXML or a DOMDocument and use it directly.

$xml = new SimpleXMLElement($response);
echo $xml->properties->property['HotelName'];
Sign up to request clarification or add additional context in comments.

4 Comments

I have edited above xml code. It contains some big code. Now how can i display all HotelName.
@Hearaman: I don't see your edited XML code. But you can simply foreach over an XML tree suing SimpleXML. See example #4 at nl.php.net/manual/en/simplexml.examples-basic.php
Thanks for your tip and suggestion
To achieve this task with given lines, we should keep that response between $xml=<<< XML and XML; How to keep my response between them. If i keep my response directly in SimpleXMLElement() output is not coming.

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.