2010-11-09 129 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