尝试从XML提要中读取值并在PHP中显示它们
问题描述:
I'm trying to display values from this xml feed: http://www.scorespro.com/rss/live-soccer.xml
In my PHP code I have the following loop but it does not display the results on my page:
<?php
$xml = simplexml_load_file("http://www.scorespro.com/rss/live-soccer.xml");
echo $xml->getName() . "<br>";
foreach($xml->children() as $item)
{
echo $item->getName() . ": " . $item->name . "<br>";
}
?>
For some reason it only shows:
rss
channel:
I'm fairly new to how XML works so any help would be much appreciated.
我正在尝试显示此xml Feed中的值: http://www.scorespro.com/rss/live-soccer.xml p>
在我的PHP代码中,我有以下循环,但它不会在我的页面上显示结果: p>
&lt;?php
$ xml = simplexml_load_file(“http:/ /www.scorespro.com/rss/live-soccer.xml");
nn echo $ xml-&gt; getName()。 “&lt; br&gt;”;
foreach($ xml-&gt; children()as $ item)
{
echo $ item-&gt; getName()。 “:”。 $ item-&gt;名称。 “&lt; br&gt;”;
}
?&gt;
code> pre>
出于某种原因,它只显示: p>
rss
channel:
code> pre>
我对XML的工作方式还很陌生,所以我们非常感谢任何帮助。 p>
div >
答
You can get actual data from $xml->channel->item
so use like below
$items = $xml->channel->item;
foreach($items as $item) {
$title = $item->title;
$link = $item->link;
$pubDate = $item->pubDate;
$description = $item->description;
}
答
you can use below code
$dom = new DOMDocument;
$dom->loadXML($url);
if (!$dom) {
echo 'Error while parsing the document';
exit;
}
$xml = simplexml_import_dom($dom);
$data = $xml->channel->item;