【问题标题】:PHP xmlReader: When to open and closePHP xmlReader:何时打开和关闭
【发布时间】:2013-08-19 13:35:41
【问题描述】:

有一个非常大的 xml 数据馈送,我正在 PHP 中解析,所以我使用 xmlReader,因为 simpleXML 每次都失败 - 只是锁定并停止。即使使用 xmlReader/simpleXML 混合代码,它仍然会失败,所以我在 xmlReader 中完成所有操作 - 不幸的是。

所以,我对在哪里打开和关闭与我的循环相关的 xmlReader 感到困惑。我需要尽可能好的内存管理。

****Open reader Here????

Foreach ($modelArray as $model)// there are 10000 models

****OR open reader Here???

    if(!$reader->open($request_url)){
        echo "Error";
        break;
    }
    while ($reader->readToNext('Product'){ // There are 500 Products  per model
       //do my node processing here. Grab nodes and add to mysql DB
    }

    return array('msg' => $msg,'addedProdsPerManu' =>$counter_addedProdsManu);

****Close Reader HERE?

}//close foreach

****OR Close Reader HERE?

非常感谢任何关于最有效内存利用的建议,因此该程序将一直运行。

谢谢

【问题讨论】:

    标签: php xml xmlreader


    【解决方案1】:

    /** */之间的评论

    /**
     * If you can, open it here.
     */
    
    Foreach ($modelArray as $model)// there are 10000 models
    
        /**
         * If you open the reader here, you are doing 10000 network requests.
         *
         * BUT depends on your needs, if the url must be get from the model, you'll
         * have to connect here.
         */
        if(!$reader->open($request_url)){
            echo "Error";
            break;
        }
    
        while ($reader->readToNext('Product'){ // There are 500 Products  per model
           //do my node processing here. Grab nodes and add to mysql DB
        }
    
        /**
         * IF you opened the connection just before the while, THEN close it here.
         */
    
        /**
         * return HERE!?? 
         * You're just checking the first model then!!
         */        
        return array('msg' => $msg,'addedProdsPerManu' =>$counter_addedProdsManu);
    
    }//close foreach
    
    /**
     * If you could open the connection before the foreach, then close it here.
     */
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-13
      • 1970-01-01
      • 2012-08-13
      • 2013-05-06
      • 1970-01-01
      • 2017-12-29
      相关资源
      最近更新 更多