doushao5047 2012-10-09 09:36
浏览 16
已采纳

在PHP中访问日期作为XML节点[重复]

Possible Duplicate:
PHP SimpleXML Namespace Problem

I'm writing a PHP script to parse an RSS feed to a webpage. Problem is accessing the date node. I think that PHP is confused because date() is a PHP function.

<?php 

  $streamData = simplexml_load_file('http://www.naps.org/index.php/rss/','SimpleXMLElement', LIBXML_NOCDATA);

  foreach ($streamData->channel->item as $item){
      $itemTitle = ($item->title);
      $itemLink = ($item->link);
      $itemDate = date_parse($item->date);
      $itemYear = $itemDate[year];
      $itemMonth = $itemDate[month];
      $itemDay = $itemDate[day];
      $itemOutputDate = $itemYear.'-'.$itemMonth.'-'.$itemDay;
      echo $itemOutputDate;
  }
?>
// echos...
--
--
--
--
--

How do I access the $item->date node?

EDIT

It's actually the <dc:date> node that I'm trying to access.

  • 写回答

3条回答 默认 最新

  • doujiu9172 2012-10-09 09:53
    关注

    The date is under the dc namespace which we can see points to http://purl.org/dc/elements/1.1/, so for example:

    $streamData = simplexml_load_file('http://www.naps.org/index.php/rss/','SimpleXMLElement', LIBXML_NOCDATA);
    
    
    foreach ($streamData->channel->item as $item)
    {
        $dc = $item->children('http://purl.org/dc/elements/1.1/');
    
        $itemDate = date_parse($dc->date);
        $itemYear = $itemDate['year'];
        $itemMonth = $itemDate['month'];
        $itemDay = $itemDate['day'];
    
        $itemOutputDate = $itemYear.'-'.$itemMonth.'-'.$itemDay;
    
        echo $itemOutputDate;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部