这里基本上有两个选项,为了便于使用,我首先将项目分配给它自己的变量:
$item = $items[$i];
然后是调试的两个选项:
var_dump($item);
echo $item->asXML();
第一行将创建一个var_dump
,它是 PHP 的,在这种情况下甚至是 SimpleXML 特定的:
class SimpleXMLElement#193 (5) {
public $title =>
string(29) "Asylum seeker system overload"
public $link =>
string(29) "http://www.abc.net.au/bestof/"
public $description =>
class SimpleXMLElement#287 (0) {
}
public $pubDate =>
string(31) "Thu, 22 Nov 2012 00:00:00 +1100"
public $guid =>
string(8) "s3638457"
}
第二行将创建一些我敢打赌对你来说很常见的东西,即 XML 本身:
<item>
<title>Asylum seeker system overload</title>
<link>http://www.abc.net.au/bestof/</link>
<description><![CDATA[
<img style="float:right;" src="http://www.abc.net.au/common/images/news_asylum125.jpg" alt="Asylum seeker detainees (ABC News)">
<p>The Australian government is preparing to allow thousands of asylum seekers to love in the community.</p>
<ul>
<li><a href="http://mpegmedia.abc.net.au/news/lateline/video/201211/LATc_FedNauru_2111_512k.mp4">Watch (4:23)</a></li><li><a href="http://www.abc.net.au/lateline/content/2012/s3638174.htm">More - Lateline</a></li>
</ul>
]]></description>
<pubDate>Thu, 22 Nov 2012 00:00:00 +1100</pubDate>
<guid isPermaLink="false">s3638457</guid>
</item>
您没有看到任何输出:
echo $items[$i];
因为该<item>
元素没有值,而只是子元素。例如
echo $items[$i]->title;
将输出字符串:
Asylum seeker system overload
我希望这会有所帮助并有所启发。您可以在此处找到演示,它还表明您可以使用foreach
:
$i = 0;
foreach ($rss->channel->item as $item)
{
if ($i++ == 2) {
var_dump($item);
echo $item->asXML(), "\n", $item->title;
}
}