Accessing date as XML node in PHP [duplicate]

∥☆過路亽.° 提交于 2019-12-23 04:28:25

问题


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.


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



回答2:


$streamData->registerXPathNamespace("dc", "http://purl.org/dc/elements/1.1/");
$nodes = $streamData->xpath("//item/dc:date");



回答3:


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

(string) $item->date



来源:https://stackoverflow.com/questions/12805378/accessing-date-as-xml-node-in-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!