我正在使用PEAR XML_Feed_Parser。 我有一些bad xml,我给它,并得到错误。loadXML无法解决的错误
DOMDocument::loadXML(): Input is not proper UTF-8, indicate encoding !
Bytes: 0xE8 0xCF 0xD3 0xD4 in Entity, line: 7
它实际上是错误编码的html - KOI8-R。
可以得到错误,但我无法处理它!
当我创建新的XML_Feed_Parser实例与 $ feed = new XML_Feed_Parser($ xml);
它调用__construct(),它看起来像
$this->model = new DOMDocument;
if (! $this->model->loadXML($feed)) {
if (extension_loaded('tidy') && $tidy) {
/* tidy stuff */
}
} else {
throw new Exception('Invalid input: this is not valid XML');
}
在哪里,我们可以看到,如果loadXML的(),那么它失败,抛出异常。
我想从loadXML()中捕获错误以跳过错误的XML并通知用户。所以,我包我的代码的try-catch像
try
{
$feed = new XML_Feed_Parser($xml);
/* ... */
}
catch(Exception $e)
{
echo 'Feed invalid: '.$e->getMessage();
return False;
}
但即使在那之后我得到这个错误
DOMDocument::loadXML(): Input is not proper UTF-8, indicate encoding !
Bytes: 0xE8 0xCF 0xD3 0xD4 in Entity, line: 7
我读过有关的loadXML(),发现
如果将空字符串作为源传递,则会生成警告。此警告不是由libxml生成的,并且不能使用libxml的错误处理函数来处理。
但不知何故,而不是警告我得到错误,停止我的应用程序。我写了我的错误处理程序,我看到这是真正的警告($ errno是2)。
所以我看到2个解决方案:
还原警告警告 - 不要 像对待错误。 (谷歌 不帮我在这里)。之后 句柄False从loadXML返回。
以某种方式捕获该错误。
任何帮助?
重复? http://stackoverflow.com/questions/2507608/error-input-is-not-proper-utf-8-indicate-encoding-using-phps-simplexml-loa – 2011-05-07 19:18:00
@ marek-sebera有点重复。我试图用iconv进行转换。但mb_detect_encoding没有检测到编码:-)它告诉我,我的不良xml是UTF-8,这显然不是真的(它是KOI8-R) – 2011-05-07 19:35:37
有趣。当我在控制台中启动带有xml文件的loadXML时,它给了我警告,我无法从中捕获到False。也许这是错误的Apache? – 2011-05-07 20:13:45