我正在通过simpleXML,它的一部分在这里:
...
<channel id="Polo TV">
<display-name lang="pl">Polo TV</display-name>
<icon src="http://m-tvguide.pl/ikony/polo.png" />
<url>http://www.programtv.interia.pl</url>
</channel>
<programme start="20141030005500 +0100" stop="20141030015000 +0100" channel="TVP 1">
<title lang="pl">Po prostu</title>
<sub-title lang="pl">http://i.iplsc.com/-/0003K96AEV7D81JK-C0.jpg</sub-title>
...
<category lang="pl">Informacja</category>
<category lang="pl">Program Tomasza Sekielskiego</category>
</programme>
<programme start="20141030015000 +0100" stop="20141030033500 +0100" channel="TVP 1">
<title lang="pl">Dorwać małego</title>
...
更多:http : //wklej.to/2zmFZ。我的PHP脚本:
<?php
$xml=simplexml_load_file("file.xml");
$channels = array();
foreach ($xml->channel as $c) {
$channels[ $c['id']->__toString() ] = $c->{'display-name'}->__toString();
}
$time = date( "YmdHi" );
foreach($xml->xpath( 'programme[@channel="TVP"]' ) as $item) {
$start = substr( (string)$item["start"], 0, -8);
$end = substr( (string)$item["stop"], 0, -8);
if ($time > $start && $time < $end) {
echo "<b><font size='3'>$item->title </font></b><br>";
echo "<u><b>Gatunek:</b></u> ".$item->{'category'}. "<br>";
echo "<u><b>Start:</b></u> " .date("G:i", strtotime($start)) . '<br>';
echo "<u><b>Koniec:</b></u> " .date("G:i", strtotime($end)) . '<br>';
echo "<u><b>Kanał:</b></u> ".$channels[ $item["channel"]->__toString() ]. "<br>";
echo "<u><b>Opis:</b></u> ".$item->desc. "<br>";
}
}
?>
从xml文件中可以看出,始终<category>
提供两个指令。我想获得第二个而不是第一个,因为现在正在这样做。
如果您有一个元素数组,则可以使用数字下标来访问它们,就像访问标准PHP数组中的元素一样。不过,最好检查是否首先设置了元素:
foreach($xml->xpath( 'programme[@channel="TVP"]' ) as $item) {
// code here...
# check whether the 'category' element is present
if (isset($item->category)) {
echo "<u><b>Gatunek:</b></u> ";
# check if there are two category items; if so, print the second
if (isset($item->category[1])) {
echo $item->category[1] . "<br>";
} else {
echo $item->category . "<br>";
}
}
// more code here...
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句