1

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.

Community
  • 1
  • 1
Jarrett Mattson
  • 1,005
  • 2
  • 9
  • 14

3 Answers3

2

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;
}
MrCode
  • 63,975
  • 10
  • 90
  • 112
2
$streamData->registerXPathNamespace("dc", "http://purl.org/dc/elements/1.1/");
$nodes = $streamData->xpath("//item/dc:date");
Leven
  • 530
  • 3
  • 10
0

If your data source is OK, then this works for me with simplexml:

(string) $item->date

Vaclav Kusak
  • 124
  • 1
  • 7