访问日期作为 PHP 中的 XML 节点 [英] Accessing date as XML node in PHP

查看:37
本文介绍了访问日期作为 PHP 中的 XML 节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
PHP SimpleXML 命名空间问题

我正在编写一个 PHP 脚本来解析网页的 RSS 提要.问题是访问日期节点.我认为 PHP 很困惑,因为 date() 是一个 PHP 函数.

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...
--
--
--
--
--

如何访问 $item->date 节点?

编辑

它实际上是我试图访问的 节点.

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

推荐答案

日期在 dc 命名空间下,我们可以看到它指向 http://purl.org/dc/element/1.1/,例如:

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

这篇关于访问日期作为 PHP 中的 XML 节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆