doushao5047 2012-10-09 17: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 17: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条)

报告相同问题?

悬赏问题

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