如何将curl xml响应转换为php中的数组



我在我的项目中使用curl。当我发送请求时,响应以如下所示的XML形式出现。如何将此xml转换为数组以呈现在网页中。

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

为什么要把它转换成数组呢?PHP非常擅长处理XML。只需在SimpleXML或DOMDocument中加载XML并直接使用它。

$xml = new SimpleXMLElement($response);
echo $xml->properties->property['HotelName'];

最新更新