如何使用SimpleXML对XML提要进行排序 [英] How to to sort a XML feed with SimpleXML
问题描述
我已经开始使用simplexml函数,该函数似乎比我尝试使用的其他其他解析器更好.我已经到了需要对项目进行分类的阶段 按传输日期排序-我尝试使用uasort,但未对项目顺序进行任何更改.
I have started to use the simplexml function that seems to work out better than the previous other parser that I have tried to use. I have got to the stage where I need to the sort the items by transmissiondate - I tried using the uasort but does not make any changes the order of the items.
有时某个程序在同一天不止一次运行-按videoID排序会更容易吗?
also some times a programme is on more than once on the same day - would it be easier to sortby videoID can any help?
对象的外观如下:
[0] => SimpleXMLElement Object
(
[VideoID] => 108059
[Genre] => Music
[ProgrammeName] => MTV
[OriginalAiringDate] => 2009-11-10T19:22:24
[TransmissionDate] => 2009-11-10T19:22:24
)
[1] => SimpleXMLElement Object
(
[VideoID] => 108395
[ExpiryDate] => 2009-12-12T23:59:59
[DateCreated] => 2009-11-12T13:28:54
[Genre] => Music
[ProgrammeName] => MTV
[OriginalAiringDate] => 2009-11-12T19:22:32
[TransmissionDate] => 2009-11-12T19:22:32
)
$xml = simplexml_load_file("data.xml");
$count = 0;
$sortItem = 0;
$dateformat = "D j M, g:ia";
$sortArray = array();
foreach($xml->CatchUp as $item){
$sortArray[$count][TransmissionDate] = $item;
if($count < 4){
print "<p>Programme Name:<strong> " . $item->ProgrammeName. "</strong></p>";
print "<p>Date Shown:<strong> " . date($dateformat, strtotime($item->TransmissionDate)). "</strong></p>";
print "<p>Description:<strong> " . trunc($item->ShortSynopsis,30, " ")."</strong></p>";
print "<p><a href='". $item->VideoID. "'>". $item->VideoID."</a></p>";
$count++;
}
}
}
asort($ sortArray);
asort($sortArray);
推荐答案
我看到了两种方法.首先是创建一个包含TransmissionDate
值的数组,然后是另一个包含相应节点的数组,然后使用array_multisort()
.这有点乏味,所以我要做的是:下载 SimpleDOM 并使用sortedXPath()
I see two ways of doing that. The first would be to create an array containing the TransmissionDate
values, then another array containing the corresponding nodes, then use array_multisort()
. This is a bit tedious, so here's what I'd do instead: download SimpleDOM and use sortedXPath()
include 'SimpleDOM.php';
$xml = simpledom_load_file("data.xml");
$dateformat = "D j M, g:ia";
foreach($xml->sortedXPath('CatchUp[ProgrammeName="MTV"]', 'TransmissionDate') as $i => $item)
{
if ($i == 4)
{
// I assume you only want the first 4
break;
}
print "<p>Programme Name:<strong> " . $item->ProgrammeName. "</strong></p>";
print "<p>Date Shown:<strong> " . date($dateformat, strtotime($item->TransmissionDate)). "</strong></p>";
print "<p>Description:<strong> " . trunc($item->ShortSynopsis,30, " ")."</strong></p>";
print "<p><a href='". $item->VideoID. "'>". $item->VideoID."</a></p>";
}
这篇关于如何使用SimpleXML对XML提要进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!