douyuqing_12345 2014-08-08 09:52
浏览 108

解析50GB xml文件,导致内存不足致命错误

I am parsing one xml file with the size near about 50GB, using below code. getting Fatal error: Out of memory (allocated 524288) (tried to allocate 5000000001 bytes)

    $xml_parser = xml_parser_create();
    xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, false);
    xml_set_element_handler($xml_parser, "startElement", "endElement");
    xml_set_character_data_handler($xml_parser, "characterData");
    if (!($fp = fopen($file, "r"))) {
        die("could not open XML input");
    }

    $e=4096;
    $file_content = fread($fp, 5000000000);

    xml_parse($xml_parser, $file_content, feof($fp));
    echo "<pre>";
    print_r($valuess);
    if(is_array($valuess)){
      mysql_connect('********', '*****', '*******');
      mysql_select_db("wp534");

    for($i=0;$i<count($valuess);$i++)
    {
    $sql = "INSERT INTO wp_xmldata (RegistreringNummerNummer, KoeretoejMaerkeTypeNavn, KoeretoejModelTypeNavn, KoeretoejVariantTypeNavn, KoeretoejTypeTypeNavn, KoeretoejOplysningFoersteRegistreringDato, DrivkraftTypeNavn, SynResultatSynsDato) values ";
    $items=array();
    $valuesArr=array();    
    //echo ($i+1).'  ------------------------<br/>';

      for($j=0;$j<count($names[$i]);$j++)
      {  //echo $names[$i][$j].'=>'.$valuess[$i][$j]."</br>";
                $a=explode("ns:",$names[$i][$j]); 
                $items[$a[1]]=$valuess[$i][$j];

      }
                $valuesArr[] = "('$items[RegistreringNummerNummer]','$items[KoeretoejMaerkeTypeNavn]','$items[KoeretoejModelTypeNavn]','$items[KoeretoejVariantTypeNavn]','$items[KoeretoejTypeTypeNavn]','$items[KoeretoejOplysningFoersteRegistreringDato]','$items[DrivkraftTypeNavn]','$items[SynResultatSynsDato]')";

    $sql .= implode(',', $valuesArr);
    /******start code for append data**********/
    /*$select="select * from wp_xmldata where RegistreringNummerNummer = '$items[RegistreringNummerNummer]'";
    //echo $fetch11;
    $select_result=mysql_query($select);
    if(mysql_num_rows($select_result) == 0)
    { //echo $sql;
      mysql_query($sql);
    }*/
    /******end code for append data**********/
    //mysql_query($sql);
    //echo $sql; 

    }

        enter code here

    }

    xml_parser_free($xml_parser);
    fclose($fp);

Anybody have a suggestion for this?

Thanks, Ankit Sanghvi

  • 写回答

1条回答 默认 最新

  • dougong2306 2014-08-08 19:23
    关注

    Your problem is not yet with the XML parser here, it's more fundamentally:

     $file_content = fread($fp, 5000000000);
    

    AFAIK in PHP strings are limited to 2GB in size. You try to obtain 50GB into that string. That simply does not work. Please consult the documentation which data does fit into which data-type. For strings you can obtain the information here:

    Note: string can be as large as up to 2GB (2147483647 bytes maximum)

    Source: http://php.net/manual/en/language.types.string.php

    You should therefore understand that xml_parse must not operate on the whole string at once, it allows to parse chunk by chunk. Here again, you should read the manual, understand what which usage those functions are meant and then write your code.

    Especially as you've running against the wall already. Time to change the code and rewrite it with a pulling logic. Or instead, take XMLReader, perhaps with XMLReaderIterator extension.

    评论

报告相同问题?

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装