duande1146 2010-11-09 15:40
浏览 103
已采纳

如何使用PHP的simplexml循环遍历XML子节点

OK, I'm having trouble looping through these sub-nodes. For this example, I'll show you the code I am using to try to grab the Amenities from a Hotel Info feed. The XML doesn't appear to be formatted great, and unfortunately I don't have control over that. Here's my code.

$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;
}

It is only returning the first amenity however.

  • 写回答

1条回答 默认 最新

  • dousi1961 2010-11-09 15:50
    关注
    $hotel_amenities = $xml->contentDataResults->hotelContent->hotelAmenities->children();
    foreach($hotel_amenities as $a)
    {
      echo $a;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,
  • ¥15 spaceclaim模型变灰色
  • ¥15 求一份华为esight平台V300R009C00SPC200这个型号的api接口文档
  • ¥15 字符串比较代码的漏洞
  • ¥15 欧拉系统opt目录空间使用100%
  • ¥15 ul做导航栏格式不对怎么改?