xml_parse退出页面。没有错误

时间:2012-11-04 13:25:12

标签: php xml xml-parsing

最近几天我正在更新脚本以使用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正在运行。解析器($ this->解析器)在另一个文件中创建。加载了(var_dumped $ this->解析器)。

创建$ this->解析器的代码(我相信这个类叫做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

3 个答案:

答案 0 :(得分:2)

我建议你使用SimpleXMLElement更加灵活。你需要做的只是循环来获得你想要的元素。

使用XML的示例

header('Content-Type: text/html; charset=utf-8');
$sxe = simplexml_load_file("log.xml", "SimpleXMLElement");

echo "<pre>";
foreach ( $sxe->dataHeader as $element ) {
    foreach ( $element as $key => $value )
        echo $key, " = ", $value, PHP_EOL;
}

echo PHP_EOL;
foreach ( $sxe->data as $record ) {
    foreach ( $record as $key => $element ) {
        foreach ( $element as $key => $value )
            echo $key, " = ", $value, PHP_EOL;
    }
}

输出

exportType = stream
exportId = 256106
rows = 1
lastChecked = 2012-11-04 14:03:06.26
lastUpdated = 2012-11-04 00:03:02.822
parserLocale = nl_NL
streamCurrency = EUR
name = befit2day.nl
description = 


recordHash = 1124208770
url = http://clicks.m4n.nl/_c?aid=14375&adid=695437&_df=true&turl=http%3A%2F%2Fbefit2day.nl%2F
title = Universeel krachtstation
description = Géén verzendkosten Verwachte levertijd 5 werkdagen Universeel krachtstation In hoogte verstelbare haltersteunen Rugleuning in 6 standen te verstellen Biceps curl steun in 3 standen te verstellen Been curl in 3 standen te verstellen (te belasten tot 60 kg) Geschikt voor halterschijven met stang opening van 20 - 31 mm Ook te gebruiken voor weighted crunches (met behulp van de kabel), tot 60 kg Ook te gebruiken voor dips Maximaal belastbaar tot 280 kg (inclusief gebruikersgewicht) Bankdrukken tot 180 kg Totaal gewicht 40 kg Geleverd exclusief halterstangen en gewichten Afmetingen krachtstation (L x B x H): 180 cm x 106 cm x 90-110 cm Afmetingen rugleuning (L x B x H): 68 cm x 28 cm x 4 cm Afmetingen curl steun (L x B x H): 28 cm x 44,5 cm x 4 cm Met aan de onderzijde gummi bekleding voor bescherming van de vloer
offerid = 
image = http://befit2day.nl/img/products/11749/103/universeel-krachtstation.jpg
price = 149.90
category = dagaanbieding
subcategory = 
stock = 1
timetoship = 
ean = 
price_shipping = 0.00
price_old = 299.90
vendor = 
category_path = 
publisher = befit2day.nl
column0 = dagaanbieding
time = 0:00
logo = http://befit2day.nl/themes/store_4/images/logo.jpg
merchantID = 20420

答案 1 :(得分:1)

检查你的日志以查看解析器正在抛出的内容,和/或尝试ini_set('display_errors',1);

您可能在文件中遇到与您尝试过的编码不兼容的字符,或者它无法识别xml文件格式正确。

如果是编码问题,请首先替换字符(如果它只有一个或两个),然后将其解析为xml,或者找到完全支持xml文件的编码。

答案 2 :(得分:1)

tail -f /var/log/apache2/error.log

刷新网站并观察“分段错误”。当一切都神奇地停止响应时,通常就是这种情况。