Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?
- // парсилка xml выгрузки gdeslon в базу SQLite
- // © skray 08.10.2016
- Error_Reporting(E_ALL);
- ini_set('display_errors', TRUE);
- @ini_set('memory_limit', '512M');
- @set_time_limit(0);
- @ini_set('max_execution_time',0);
- @ini_set('set_time_limit',0);
- @ob_end_flush();
- $start_time = microtime(true);
- $g=0;
- $_db="data/slonxml.db"; // название будущей базы // папка data в данном примере должна уже быть создана
- $_xml="slon.xml"; // xml файл, который нужно распарсить
- $xml_td=array("id", "_id", "merchant_id", "gs_product_key", "article", "gs_category_id", "original_picture", "name", "description", "vendor", "model", "url", "price", "categoryid", "param");
- $cat_td=array("id", "_id","parent_id","name");
- function ar($ar,$at)
- {
- $zip=preg_replace("/(.*)$at$/",'\1',$ar);
- $zip=explode($at,$zip);
- return $zip;
- }
- function str($ar,$at)
- {
- $zip=preg_replace("/(.*)$at$/",'\1',$ar);
- return $zip;
- }
- function kv($ssl)
- {
- $ssl=preg_replace("/\"|\'/",'',$ssl);
- return $ssl;
- }
- function baza($xml_td,$cat_td)
- {
- global $_db,$dbx;
- if (!file_exists($_db))
- {
- $xml_list="";$cat_list="";
- foreach($xml_td as $k) {$lis=$k=='id'?"INTEGER PRIMARY KEY":"TEXT";$xml_list.="$k $lis,";}
- foreach($cat_td as $k) {$lis=$k=='id'?"INTEGER PRIMARY KEY":"TEXT";$cat_list.="$k $lis,";}
- $xml_list=str($xml_list,",");$cat_list=str($cat_list,",");
- $dbx = new SQLite3($_db);
- $dbx->busyTimeout(5000);
- $dbx->exec('PRAGMA journal_mode=WAL;');
- $dbx->exec("CREATE TABLE IF NOT EXISTS cat ($cat_list);");
- $dbx->exec("CREATE TABLE IF NOT EXISTS xml ($xml_list);");
- $dbx->close();
- }
- $dbx = new SQLite3($_db);
- $dbx->busyTimeout(5000);
- $dbx->exec('PRAGMA journal_mode=WAL;');
- }
- baza($xml_td,$cat_td);
- $x_id = array_shift($xml_td);
- $c_id = array_shift($cat_td);
- $reader = new XMLReader();
- $reader->open($_xml);
- $dbx->exec('BEGIN IMMEDIATE;');
- while ($reader->read())
- {
- if ($reader->nodeType == XMLREADER::ELEMENT&&$reader->localName == 'categories')
- {
- $category = new SimpleXMLElement($reader->readouterXML());
- foreach($category as $lin)
- {
- $cats=$lin->attributes();
- $cat="'".$cats['id']."','".$cats['parent_id']."','".$lin."'";
- $dbx->exec("INSERT INTO cat (_id,parent_id,name) VALUES ($cat);");
- }
- }
- if ($reader->nodeType == XMLREADER::ELEMENT&&$reader->localName == 'offer')
- {
- $offer = new SimpleXMLElement($reader->readouterXML());
- $attr=$offer->attributes();
- $params = $offer->param;
- $name= kv($offer->name);
- $price= kv($offer->price);
- $picture = $offer->picture;
- $original_picture = $offer->original_picture;
- $description = kv($offer->description);
- $vendor = kv($offer->vendor);
- $model = kv($offer->model);
- $url = $offer->url;
- $gs_category_id = $attr['gs_category_id'];
- $merchant_id = $attr['merchant_id'];
- $article=$attr['article'];
- $gs_product_key=$attr['gs_product_key'];
- $_id=$attr['id'];
- $categoryid= $offer->categoryId;
- $param1="";$param2="";
- foreach ($params as $atr){$param1.=$atr['name']."~";$param2.=$atr."~";}
- $param1=ar($param1,"~");$param2=ar($param2,"~");
- $param=serialize(array_combine($param1,$param2));
- $ntb="";$array="";
- foreach($xml_td as $nm){$ntb.=$nm.",";$array.="'".$$nm."',";}
- $ntb=str($ntb,",");$array=str($array,",");
- $dbx->exec("INSERT INTO xml ($ntb) VALUES ($array);");
- if($g/200==intval($g/200))
- {
- $dbx->exec('COMMIT;');
- $dbx->exec('BEGIN IMMEDIATE;');
- }
- $g++;
- }
- }
- $dbx->exec('COMMIT;');
- $dbx->close();
- $exec_time = round(microtime(true) - $start_time, 3);
- $memory = round(memory_get_usage() / 1024, 3);
- echo "<br><hr><br>Загружено $g за $exec_time сек. | Memory: $memory Kb.";
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement