php - 如何读取此 xml,获取 "parser error : CData section not finished"

标签 php xml simplexml cdata

我正在尝试读取此 xml: xml rss file

但没有成功..有这个错误

    Warning: simplexml_load_file(): http://noticias.perfil.com/feed/:232: parser error : CData section not finished <p>La sola lectura de los datos estadísticos desp in D:\xampp\FerreWoo\scrap-rvnot.php on line 43

    Warning: simplexml_load_file(): Isis, con lo que habría logrado un nuevo respaldo a sus proyectos terroristas. in D:\xampp\FerreWoo\scrap-rvnot.php on line 43

    Warning: simplexml_load_file(): ^ in D:\xampp\FerreWoo\scrap-rvnot.php on line 43

我正在使用这段代码:

   $feed = simplexml_load_file($urls, null, LIBXML_NOCDATA);

我也尝试了 cURL,但同样的错误仍然存​​在。

我知道可能 de xml 文件不正确...但必须有一种方法来读取它,对吧?

最佳答案

您在该 XML 中包含一些无效字符。试试下面的代码

$url    = 'http://noticias.perfil.com/feed/';
$html   = file_get_contents($url);
$invalid_characters = '/[^\x9\xa\x20-\xD7FF\xE000-\xFFFD]/';
$html = preg_replace($invalid_characters, '', $html);

$xml = simplexml_load_string($html);

//test purpose part 
$encode = json_encode($xml);
$decode = json_decode($encode, true);
print_r($decode);

希望对你有帮助

关于php - 如何读取此 xml,获取 "parser error : CData section not finished",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44250530/

相关文章:

php - PHP 中的 bool 条件

xml - 使用 groovy 更新 xml 文件时保留格式

php - 标记为 UTF-16 的文档但在 Entity PHP 错误中包含 UTF-8 内容

php - Cron Job (PHP) --> 选择明年的记录不起作用

android - 早于 4.2 的 android 中从右到左的元素方向

xml - 如何将快捷方式命名空间添加到我的 xml 文档中

php - 访问 SimpleXMLElement 对象的某些属性

PHP SimpleXMLElement 不对实体进行引号

php - Json 到带有希腊字符的 xml

php - PhoneGap FileTransfer 和服务器端脚本