阅读RSS提要
I need to read an XML file, i watched some tutorials and tried different sollutions, but for some reason I can't figure out why it doenst work.
The XML file that I want to read: http://www.voetbalzone.nl/rss/rss.xml
This is the code that im using:
$xml= "http://www.voetbalzone.nl/rss/rss.xml"
for ($i = 0; $i < 10; $i++)
{
$title = $xml->rss->channel->item[$i]->title;
}
The error I get: Premature end of data in tag
我需要阅读一个XML文件,我看了一些教程并尝试了不同的解决方案,但出于某种原因,我可以' 弄清楚它为什么要工作。 p>
我想读的XML文件: http://www.voetbalzone.nl/rss/rss.xml p>
这是我使用的代码: p> \ n
$ xml =“http://www.voetbalzone.nl/rss/rss.xml"
nnfor( $ i = 0; $ i&lt; 10; $ i ++)
{\ n $ title = $ xml-&gt; rss-&gt; channel-&gt; item [$ i] - &gt; title;
}
code> pre>
错误I get:标记 p>
div>中的数据提前结束
It works for me like this:
<?php
$xml = simplexml_load_file("http://www.voetbalzone.nl/rss/rss.xml");
for ($i = 0; $i < 10; $i++)
{
$title = $xml->channel->item[$i]->title;
}
?>
Note that you are overwriting the variable $title
each time, so that you will have the title of the 10. element in it after the loop finished [I assume that is not what you want?]
To get all 'item'-Elements inside 'channel' as an Array to iterate through you can use xpath like this:
<?php
$xml = simplexml_load_file("http://www.voetbalzone.nl/rss/rss.xml");
$item_array = $xml->xpath("//rss/channel/item");
foreach($item_array as $item) {
echo $item->title . "
";
}
?>
I would suggest to read about php's SimpleXML here: http://php.net/manual/en/book.simplexml.php