远程XML文件写得不好导致解析错误

时间:2013-08-09 15:07:20

标签: php xml xml-parsing rss html-parsing

我正在编写RSS类型的阅读器网页来解析某些游戏网站的信息。

其中一个游戏RSS提要写得不好。

他们没有费心将描述包装到CDATA中,而是使用simplexml_load_file解析错误。

这是我写的解析它的函数:

 function displayAll($url) {

 $url = "https://www.game.com/newsfeed/rss.vm";
 $game = simplexml_load_file($url);

 $item = $game->rss->channel->item;
 foreach ($item as $items) {

 echo '<li>';
 echo ''.$items->title.'';
 echo ''.$items->description.'';
 echo ''.$items->link.'';
 echo '</li>';

  }
 }

我收到以下错误:

 Warning: simplexml_load_file() [function.simplexml-load-file]: https://www.game.com/newsfeed/rss.xml:15: parser error : 
 Entity 'nbsp' not defined in /results.php on line 27
 Warning: simplexml_load_file() [function.simplexml-load-file]: https://www.game.com/newsfeed/rss.xml:20: parser error : 
 Entity 'nbsp' not defined in /results.php on line 27

获得相同错误的多个部分都围绕着在feed中写得不好的html。

我在问如何解决这个问题,有没有办法在解析之前将html转换回xml描述中的letters / spaces / etc标签?

任何建议都将不胜感激。

1 个答案:

答案 0 :(得分:1)

试试这个

$data = file_get_contents($url);
$data = str_replace(array('<description>','</description>'), array('<description><![CDATA[',']]></description>'), $data);

$game = simplexml_load_string($data);
相关问题