0

最近几天我正在更新一个脚本以使用 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

4

3 回答 3

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
于 2012-11-10T02:31:54.047 回答
1

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

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

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

于 2012-11-07T16:08:43.857 回答
1
tail -f /var/log/apache2/error.log

刷新网站并注意“Segmentation Fault”。当一切都神奇地停止响应时,通常会出现这种情况。

于 2012-11-08T15:26:56.663 回答