2011-07-07 48 views
0

我在我的項目中使用了捲曲。當我發送請求時,響應以XML的形式出現在下面。我怎樣才能將這個XML轉換成數組來呈現在網頁中。如何在curl中將curl 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

在PHP ............... – Hearaman

回答

5

爲什麼它轉換成數組呢? PHP對XML非常好。只需加載SimpleXMLDOMDocument中的XML並直接使用它。

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

我編輯了上面的xml代碼。它包含一些大代碼。現在我怎麼顯示所有的HotelName。 – Hearaman

+0

@Hearaman:我沒有看到您編輯的XML代碼。但是你可以簡單地通過一個XML樹起訴SimpleXML。請參閱http://nl.php.net/manual/en/simplexml.examples-basic.php –

+0

上的示例#4感謝您的提示和建議 – Hearaman