我有一个XML页面holidays.xml
和一个php页面holiday(simplexml).php
我试图在php页面的表格中显示XML页面的详细信息但无效,但没有给出错误消息但是没有内容显示,有人能指出我正确的方向吗?
<?php
// load the xml file into a simplexml instance variable
$holiday = simplexml_load_file('holidays.xml');
// draw a table and column headers
echo "<table border=\"1\">";
echo " <th>Title</td>
<th>Link</td>
<th>Description</td>
<th>Published Date</td>
<th>Guid</th>";
// iterate through the item nodes displaying the contents
foreach ($holiday->item as $holiday) {
echo "<tr>";
echo "<td>{$holiday->title}</td>";
echo "<td>{$holiday->link}</td>";
echo "<td>{$holiday->description}</td>";
echo "<td>{$holiday->pubDate}</td>";
echo "<td>{$holiday->guid}</td>";
echo "</tr>\n";
}
echo "</table>";
?>
holidays.xml
<?xml version="1.0" encoding="ISO-8859-1" ?>
<rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/">
<channel>
<title>Special offers on exotic holidays</title>
<link>http://www.numyspace.co.uk/~cgel1/holidays/special_offers.html</link>
<description>The best site for special offers on exotic holidays. We offer many special offers in the way of holiday discounts,
free room upgrades , free flight upgrades and free child places.</description>
<language>en-gb</language>
<pubDate>Sun, 13 Feb 2011 00:00:00 GMT</pubDate>
<image>
<title>Special offers for exotic holidays</title>
<link>http://www.numyspace.co.uk/~cgel1/holidays/special_offers.html</link>
</image>
<item>
<title>Luxurious Jamaican holidays | 40% Discount On Accommodation - Book Now!</title>
<link>http://www.numyspace.co.uk/~cgel1/holidays/Jamaica.html</link>
<description>7 nights at The Golden Palm, Montego Bay travelling from Newcastle with Fly Jamaica</description>
<pubDate>Sun, 13 Feb 2011 11:58:17 GMT</pubDate>
<guid>http://www.numyspace.co.uk/~cgel1/holidays/Jamaica.html</guid>
</item>
<item>
<title>Barbados holidays | Royal Wedding special - Free room upgrade</title>
<link>http://www.numyspace.co.uk/~cgel1/holidays/Barbados.html</link>
<description>10 nights at 5* Pride of Barbados hotel for only £950, travelling on BA from Gatwick</description>
<pubDate>Mon, 14 Feb 2011 13:01:10 GMT</pubDate>
<guid>http://www.numyspace.co.uk/~cgel1/holidays/Barbados.html</guid>
</item>
</channel>
</rss>
答案 0 :(得分:2)
您忘记了foreach循环中的频道节点。
尝试放
foreach ($holiday->channel->item as $holiday) {
echo "<tr>";
echo "<td>{$holiday->title}</td>";
echo "<td>{$holiday->link}</td>";
echo "<td>{$holiday->description}</td>";
echo "<td>{$holiday->pubDate}</td>";
echo "<td>{$holiday->guid}</td>";
echo "</tr>\n";
}