分析XML时出现语法错误


Syntax Error while Parsing XML

我试图以XML的形式获取网站的rss提要并尝试显示它,但我得到了语法错误,我的整个代码如下:

<?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解析错误:语法错误位置:localhost/rssfed.php

有人能告诉我我做错了什么吗?

感谢

由于您没有输出正确的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>
 */
?>