【发布时间】:2012-11-04 13:25:11
【问题描述】:
最近几天我正在更新一个脚本以使用 Joomla 2.5。它几乎完成了,但有一件事我还没有解决。这很奇怪。
该脚本有一个解析附属 XML 的 cron。为此,它使用 PHP 函数 xml_parse,如下所示:
if (!($fp = @$file_function($url, 'rb'))) {
$this->error("Cannot open {$url}");
return;
}
while (($data = fread($fp, 8192))) {
if ( defined ('LIBXML_BUG') ) {
# fix voor LIBXML BUG
$data=str_replace("&","XMLLIBHACK",$data);
}
if (!xml_parse($this->parser, $data, feof($fp))) {
printf('XML error in %s at line %d column %d',
$url,
xml_get_current_line_number($this->parser),
xml_get_current_column_number($this->parser));
unset ($this->items);
}
}
xml_parser_free( $this->parser );
如前所述,问题在于 xml_parse 函数。在这一行,整个页面/脚本停止工作,只返回写在这一行之上的内容。当 error_reporting 为 E_ALL 且 display_errors 为 On 时,它不会返回任何错误。故意创建错误时,我看到了错误,因此 error_reporting 正在工作。 parser($this->parser) 是在另一个文件中创建的。已加载(var_dumped $this->parser)。
创建$this->parser的代码(我相信这个类叫MagpieRSS):
function create_parser($out_enc, $in_enc, $detect) {
if ( substr(phpversion(),0,1) == 5) {
$parser = $this->php5_create_parser($in_enc, $detect);
}
else {
$parser = $this->php4_create_parser($in_enc, $detect);
}
if ($out_enc) {
$this->encoding = $out_enc;
xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, $out_enc);
}
return $parser;
}
/**
* Instantiate an XML parser under PHP5
*
* PHP5 will do a fine job of detecting input encoding
* if passed an empty string as the encoding.
*
* All hail libxml2!
*
*/
function php5_create_parser($in_enc, $detect) {
// by default php5 does a fine job of detecting input encodings
if(!$detect && $in_enc) {
return xml_parser_create($in_enc);
}
else {
return xml_parser_create('');
}
}
/**
* Instaniate an XML parser under PHP4
*
* Unfortunately PHP4's support for character encodings
* and especially XML and character encodings sucks. As
* force to UTF-8 use admin settings to change this
*/
function php4_create_parser($in_enc, $detect) {
if ( $detect ) {
$in_enc = 'UTF-8';
}
return xml_parser_create($in_enc);
}
我没有解决这个问题的想法。我尝试了不同的编码(ISO、UTF-8 等),检查了 $data 但一切似乎都是文件。
可在此处找到示例 XML 文件:http://pastebin.com/wT1pVZLQ
【问题讨论】:
-
我已经尝试过您的脚本(带有提供的数据),它对我来说并没有损坏。我在
xml_parser_free( $this->parser );之后执行了几行代码,效果很好。你能把全班都发过来让我看详细吗? -
你在哪里设置错误报告设置?在代码中,还是在 php.ini 中?
-
Error_reporting 和显示错误是在 php.ini 中设置的,我在代码本身中尝试过,但没有帮助。
-
您使用的是什么版本的 PHP ?您介意使用 SimpleXMLElement 或 DOMDocument 的解决方案吗?
-
PHP 版本为 5.2.17。而且我认为重写为 SimpleXML 是最好的。我想我自己可以做到。只有很多工作:P
标签: php xml xml-parsing