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

时间:2011-07-07 12:08:26

标签: php xml arrays curl

我在我的项目中使用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>

1 个答案:

答案 0 :(得分:6)

为什么要将它转换为数组呢? PHP非常适合XML。只需在SimpleXMLDOMDocument中加载XML并直接使用即可。

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