piterskiy

Пример парсера XML-файлов

Oct 8th, 2016
24,820
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?
  2.  
  3. Error_Reporting(E_ALL);
  4. ini_set('display_errors', TRUE);
  5. @ini_set('memory_limit', '512M');
  6. @set_time_limit(0);
  7. @ini_set('max_execution_time',0);
  8. @ini_set('set_time_limit',0);
  9. @ob_end_flush();
  10. $start_time = microtime(true);
  11. $g=0;
  12. $_db="data/slonxml.db";  // название будущей базы, папка data в данном примере должна уже быть создана
  13. $_xml="slon.xml";        // xml файл, который нужно распарсить
  14. $xml_td=array("id", "_id", "merchant_id", "gs_product_key", "article", "gs_category_id", "original_picture", "name", "description", "vendor", "model", "url", "price", "categoryid", "param");
  15. $cat_td=array("id", "_id","parent_id","name");
  16.  
  17. function ar($ar,$at)
  18. {
  19.     $zip=preg_replace("/(.*)$at$/",'\1',$ar);
  20.     $zip=explode($at,$zip);
  21.     return $zip;
  22. }
  23.  
  24. function str($ar,$at)
  25. {
  26.     $zip=preg_replace("/(.*)$at$/",'\1',$ar);
  27.     return $zip;
  28. }
  29.  
  30. function kv($ssl)
  31. {
  32.     $ssl=preg_replace("/\"|\'/",'',$ssl);
  33.     return $ssl;
  34. }
  35.  
  36. function baza($xml_td,$cat_td)
  37. {
  38.     global $_db,$dbx;
  39.     if (!file_exists($_db))
  40.     {
  41.         $xml_list="";$cat_list="";
  42.         foreach($xml_td as $k) {$lis=$k=='id'?"INTEGER PRIMARY KEY":"TEXT";$xml_list.="$k $lis,";}
  43.         foreach($cat_td as $k) {$lis=$k=='id'?"INTEGER PRIMARY KEY":"TEXT";$cat_list.="$k $lis,";}
  44.         $xml_list=str($xml_list,",");$cat_list=str($cat_list,",");
  45.         $dbx = new SQLite3($_db);
  46.         $dbx->busyTimeout(5000);
  47.         $dbx->exec('PRAGMA journal_mode=WAL;');
  48.         $dbx->exec("CREATE TABLE IF NOT EXISTS cat ($cat_list);");
  49.         $dbx->exec("CREATE TABLE IF NOT EXISTS xml ($xml_list);");
  50.         $dbx->close();
  51.     }
  52.     $dbx = new SQLite3($_db);
  53.     $dbx->busyTimeout(5000);
  54.     $dbx->exec('PRAGMA journal_mode=WAL;');
  55. }
  56.  
  57. baza($xml_td,$cat_td);
  58. $x_id = array_shift($xml_td);
  59. $c_id = array_shift($cat_td);
  60. $reader = new XMLReader();
  61. $reader->open($_xml);
  62. $dbx->exec('BEGIN IMMEDIATE;');
  63.  
  64. while ($reader->read())
  65. {
  66.     if ($reader->nodeType == XMLREADER::ELEMENT&&$reader->localName == 'categories')
  67.     {
  68.         $category = new SimpleXMLElement($reader->readouterXML());
  69.         foreach($category as $lin)
  70.         {
  71.             $cats=$lin->attributes();
  72.             $cat="'".$cats['id']."','".$cats['parent_id']."','".$lin."'";
  73.             $dbx->exec("INSERT INTO cat (_id,parent_id,name) VALUES ($cat);");         
  74.         }
  75.     }      
  76.     if ($reader->nodeType == XMLREADER::ELEMENT&&$reader->localName == 'offer')
  77.     {
  78.         $offer = new SimpleXMLElement($reader->readouterXML());
  79.         $attr=$offer->attributes();
  80.         $params = $offer->param;       
  81.         $name= kv($offer->name);
  82.         $price= kv($offer->price);
  83.         $picture = $offer->picture;
  84.         $original_picture = $offer->original_picture;
  85.         $description = kv($offer->description);
  86.         $vendor = kv($offer->vendor);
  87.         $model = kv($offer->model);
  88.         $url = $offer->url;
  89.         $gs_category_id = $attr['gs_category_id']; 
  90.         $merchant_id = $attr['merchant_id'];
  91.         $article=$attr['article'];
  92.         $gs_product_key=$attr['gs_product_key'];
  93.         $_id=$attr['id'];
  94.         $categoryid= $offer->categoryId;
  95.         $param1="";$param2="";
  96.         foreach ($params as $atr){$param1.=$atr['name']."~";$param2.=$atr."~";}
  97.         $param1=ar($param1,"~");$param2=ar($param2,"~");
  98.         $param=serialize(array_combine($param1,$param2));
  99.         $ntb="";$array="";
  100.         foreach($xml_td as $nm){$ntb.=$nm.",";$array.="'".$$nm."',";}
  101.         $ntb=str($ntb,",");$array=str($array,",");
  102.         $dbx->exec("INSERT INTO xml ($ntb) VALUES ($array);"); 
  103.         if($g/200==intval($g/200))
  104.         {
  105.             $dbx->exec('COMMIT;'); 
  106.             $dbx->exec('BEGIN IMMEDIATE;');
  107.         }              
  108.         $g++;                  
  109.     }
  110. }
  111.        
  112. $dbx->exec('COMMIT;');
  113. $dbx->close(); 
  114.  
  115. $exec_time = round(microtime(true) - $start_time, 3);
  116. $memory = round(memory_get_usage() / 1024, 3);
  117. echo "<br><hr><br>Загружено $g за $exec_time сек. | Memory: $memory Kb.";
  118.  
  119.  
  120. ?>
Add Comment
Please, Sign In to add comment