2012-08-24 28 views
0

我試圖讓一個網站作爲XML的RSS源,並試圖顯示它,但我歌廳語法錯誤,我的整個代碼如下:語法錯誤在解析XML

<?php 
    header('Content-type: text/xml'); 
    $feeds = file_get_contents('http://rss.news.yahoo.com/rss/topstories'); 
    $xml = simplexml_load_string($feeds); 
    foreach ($xml->channel->item as $item) { 
     print $item->title; 
    } 
?> 

我得到的錯誤是

XML解析錯誤:語法錯誤的位置:本地主機/ rssfeed.php

誰能指導我什麼,我做錯了什麼?

感謝

+1

做,如果你註釋掉'頭()'你得到什麼輸出? – andrewsi

+0

我得到的標題是正確的,但我想看到整個XML結構,我如何查看它? –

+0

代碼請理解你的問題? –

回答

0

你獲得XML解析錯誤,因爲你沒有正確的輸出XML,通過設置頁眉內容類型的瀏覽器期待有效​​的XML。

試試這個:

<?php 
header('Content-type: text/xml'); 

//Load your feed (input) 
$feed = file_get_contents('http://rss.news.yahoo.com/rss/topstories'); 
$feed_xml = simplexml_load_string($feed); 

//Create a new SimpleXMLElement object (for output) 
$xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><news/>'); 

//Loop each item from $feed_xml->channel->item 
foreach ($feed_xml->channel->item as $item) { 
    //Create a story node for each item 
    $node = $xml->addChild('story'); 
    //Loop each items & add it to the node if its a title or description 
    foreach($item as $key=>$value){ 
     if($key == 'title'){$node->addChild($key, $value);} 
     if($key == 'description'){$node->addChild($key, $value);} 
    } 
} 

//Use dom document to format the output nicly, not required 
$dom = new DOMDocument('1.0'); 
$dom->formatOutput = true; 
$dom->loadXML($xml->asXML()); 

echo $dom->saveXML(); 
/**Output 
<?xml version="1.0" encoding="UTF-8"?> 
<news> 
    <story> 
    <title>Ryan says Obama undercuts welfare reform law</title> 
    <description>Republican vice presidential candidate Paul Ryan says President Barack Obama wants to ease work requirements for welfare recipients even though that claim has been largely debunked by independent fact checkers.</description> 
    </story> 
    <story> 
    <title>Greenpeace activists storm Russian oil rig</title> 
    <description>&lt;p&gt;&lt;a href="http://news.yahoo.com/greenpeace-activists-storm-russian-oil-rig-063813773.html"&gt;&lt;img src="http://l3.yimg.com/bt/api/res/1.2/mSCkTQFkrkzOGPzb3sGLVw--/YXBwaWQ9eW5ld3M7Zmk9ZmlsbDtoPTg2O3E9ODU7dz0xMzA-/http://media.zenfs.com/en_us/News/ap_webfeeds/2c763e62d83a2717190f6a7067003ab4.jpg" width="130" height="86" alt="Greenpeace activists including Executive Director of Greenpeace International, Kumi Naidoo, board energy giant Gazprom's Arctic oil platform Prirazlomnaya off the North-eastern coast of Russia in the Pechora Sea on Friday, Aug. 24, 2012. Greenpeace activists have stormed a floating oil rig in Russia’s Pechora Sea to protest oil drilling in the Arctic, the environmental organization said on Friday. (AP Photo/ Denis Sinyakov, Greenpeace)" align="left" title="Greenpeace activists including Executive Director of Greenpeace International, Kumi Naidoo, board energy giant Gazprom's Arctic oil platform Prirazlomnaya off the North-eastern coast of Russia in the Pechora Sea on Friday, Aug. 24, 2012. Greenpeace activists have stormed a floating oil rig in Russia’s Pechora Sea to protest oil drilling in the Arctic, the environmental organization said on Friday. (AP Photo/ Denis Sinyakov, Greenpeace)" border="0" /&gt;&lt;/a&gt;Greenpeace activists stormed a floating Russian oil rig early Friday in the open sea to protest oil drilling in the Arctic, the environmental organization said.&lt;/p&gt;&lt;br clear="all"/&gt;</description> 
    </story> 
    ... 
    ... 
</news> 
*/ 
?>