【问题标题】:xml_parse quits page. No errorsxml_parse 退出页面。没有错误
【发布时间】: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


【解决方案1】:

我建议您使用更灵活的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

【讨论】:

    【解决方案2】:

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

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

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

    【讨论】:

      【解决方案3】:
      tail -f /var/log/apache2/error.log
      

      刷新网站并注意“Segmentation Fault”。这通常是一切都神奇地停止响应的情况。

      【讨论】:

      • 错误日志没有显示任何分段错误或任何错误。看起来 PHP 只是停止执行文件...
      • 恐怕 PHP 不会停止,只是崩溃,因此没有错误。你用的是什么版本的PHP?
      猜你喜欢
      • 2013-10-09
      • 2022-08-02
      • 2015-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-27
      • 2012-08-22
      相关资源
      最近更新 更多