2012-03-08 61 views
0

我有一個XML頁面holidays.xml和一個PHP頁面holiday(simplexml).php我試圖在PHP頁面的表格中顯示XML頁面的詳細信息,但無濟於事,沒有給出錯誤消息,但沒有給出內容顯示,任何人都可以指向正確的方向嗎?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> 

回答

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"; 
} 
+1

曾爲完美的感謝。 – 2012-03-08 23:31:06