2010-11-09 133 views
2

好的,我無法循環這些子節點。在這個例子中,我將向您展示我正在使用的代碼來嘗試從酒店信息Feed中獲取設施。 XML看起來沒有很好的格式,不幸的是我無法控制。這是我的代碼。如何使用PHP的simplexml循環瀏覽XML子節點

$xml = simplexml_load_file("http://www.2-20.com/hotelRoomSearchDetails.cfm?pnum_hotel_seq_id=210&pchr_room_type=STUDIO%22") 
$hotel_amenities = $xml->contentDataResults->hotelContent->hotelAmenities; 

foreach($hotel_amenities as $a){ 
    echo $a->amenity; 
} 

但它只是返回第一個設施。

+0

這可能有所幫助:http://stackoverflow.com/q/871422/351893 – JochenJung 2014-03-06 15:09:33

回答

19
$hotel_amenities = $xml->contentDataResults->hotelContent->hotelAmenities->children(); 
foreach($hotel_amenities as $a) 
{ 
    echo $a; 
} 
+0

非常感謝! – 2010-11-09 15:52:40