使用SimpleXML显示最新的XML提要条目


Showing newest XML feed entry using SimpleXML

我已经搜索过了,但没有找到我想要的东西。我使用simpleXML来解析我制作的RSS提要,它非常适合只显示一个条目。我正在尝试修改此方法,以便只提取最近更新的条目。我该如何更新它,或者只是提取最新的条目?

这就是我现在所拥有的,它解析RSS提要并只显示一个条目,但是,这是我被卡住的地方,因为我只想显示最新的条目。

这只是解析提要的最相关代码的一个片段。

//Set initial output to false
    $tData = false;
for($i = 0; $i < 1; $i++){
    $location = $xml->reports[$i]->location;
    $upperCase = strtoupper($location);
    $report = $xml->reports[$i]->report;
    $timestamp = $xml->reports[$i]->timestamp;
    $updateTime = DATE("g:i A", STRTOTIME($timestamp));
// Set table style
   $tableStyle = "width: 100%; margin:0px auto; background-color:{$bkgColor};";
   $td1Style = "{$tbrdr};{$sbrdr}; text-align:center; font-size: 11px; background-image:url({$imagesDir}headerbgd2.gif); color:{$dtColor};";
   $td2Style = "{$sbrdr}; text-align:center; font-size: 12px; padding: 1px 0px 1px 0px; background-color:{$bkgColor};";
   $td3Style = "{$sbrdr}; {$bbrdr}; text-align:center; background-color:{$bc};";
 // construct data for table display
    $tData .= "<table style='{$tableStyle}' cellpadding='0' cellspacing='0'>'n";
    $tData .= "<tbody>'n";
    $tData .= "  <tr><td style='{$td1Style}'>LATEST LOCAL STORM REPORT</td></tr>'n";
    $tData .= "  <tr><td style='{$td2Style}'><b>{$report}</b>&nbsp;&nbsp; - &nbsp;&nbsp;<span style='color: rgb(204, 102, 0);'>{$upperCase} - {$updateTime}</span></td></tr>'n";
    $tData .= "  <tr><td style='{$td3Style}'><a href='wxmesqLSR.php' title='Click to view the details'>Click here for details</a></td></tr>'n";
    $tData .= "</tbody>'n";
    $tData .= "</table>'n";
    $tData .=  $afterTable;
 }

编辑:XML文件示例

<?xml version="1.0"?>
<entrys>
  <reports>
    <timestamp>Thu, 11 Jul 2013 23:19:39 -0500</timestamp>
    <name>Mesquite Weather</name>
    <location>Mesquite</location>
    <report>GENERAL</report>
    <description>Official MW test</description>
  </reports>
  <reports>
    <timestamp>Fri, 12 Jul 2013 00:44:39 -0500</timestamp>
    <name>Mesquite Weather</name>
    <location>Sunnyvale</location>
    <report>DOWNED POWER LINES</report>
    <description>Just an official MW test</description>
  </reports>
</entrys>

-谢谢!

您必须进行一些手动日期比较才能找到正确的条目,除非它们已经在xml中排序。所以基本上你的工作流程必须是:

  1. 在所有reports节点上循环并读取其时间戳
  2. 将时间戳转换为可比较的值(使用strtotimedate),并将它们全部进行比较,以查找最新的reports
  3. 拿起被确定为最新的reports并使用您现有的功能

如果它们在XML中按时间戳排序,那么最近的条目将是第一个或最后一个。根据你的例子,第一个不是你要找的,我怀疑它可能是最后一个。如果是这种情况,只需找到reports元素的数量(使用XPath count(//reports)(或类似方法最简单),然后读取具有此索引的reports元素。

最新条目是转换为Unix时间戳时具有最高时间戳的条目。

在您当前的文档中,情况并非如此。因此,让我们创建一个Unix时间戳数组:

$timestamps = array_map('strtotime', $xml->xpath('/*/reports/timestamp'));

为了能够对这些进行排序,报告也需要进行排序:

$reports    = $xml->xpath('/*/reports');

现在你有了一组时间戳:

Array
(
    [0] => 1373602779
    [1] => 1373607879
)

以及一系列报告:

Array
(
    [0] => SimpleXMLElement Object
        (
            [timestamp] => Thu, 11 Jul 2013 23:19:39 -0500
            [name] => Mesquite Weather
            [location] => Mesquite
            [report] => GENERAL
            [description] => Official MW test
        )
    [1] => SimpleXMLElement Object
        (
            [timestamp] => Fri, 12 Jul 2013 00:44:39 -0500
            [name] => Mesquite Weather
            [location] => Sunnyvale
            [report] => DOWNED POWER LINES
            [description] => Just an official MW test
        )
)

根据另一个数组的值对一个数组进行排序已经在中进行了概述

  • 如何在php中对多维数组进行排序

同样的原理也适用于simplexml中的场景,不同之处在于您需要首先创建这些数组——这就是我已经演示过的。

希望这对你有帮助。simplexml排序的相关问题有:

  • SimpleXML和PHP中的Xpath返回的排序结果
  • Usort不完全排序
  • simpleXML的PHP排序问题