dongyao4419 2013-08-17 05:00
浏览 65

PHP xmlReader:何时打开和关闭

Have a very large xml data feed I am parsing in PHP so Im using xmlReader because simpleXML fails everytime - just locks up and stops. Even with an xmlReader/simpleXML hybrid code, it still fails so Im doing it all in xmlReader - unfortunately.

So, Im confused on where to open and close xmlReader in relation to my loops. I need the best memory managment possible.

****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?

Any advice would be greatly appreciated for the most efficient memory utilization so this program will run all the way through.

Thank you

  • 写回答

1条回答 默认 最新

  • dpp3047 2013-08-17 07:19
    关注

    Comments below between /** */

    /**
     * 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.
     */
    
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?