piterskiy

XML to MultiKey

Apr 28th, 2017
24,876
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. $fileXML = 'DOFQ89AlkJobBIlRJJDgQ6xyBEQllQniogFHwHd5.xml'; // Название файла
  4. $separator = '|'; // разделитель мултикеев
  5.  
  6. $z = new XMLReader;
  7. $z->open($fileXML);
  8.  
  9. $doc = new DOMDocument;
  10.  
  11. // переходим к первому offer
  12. while ($z->read() && $z->name !== 'offer');
  13.  
  14. // теперь на нужной глубине, переходим к следующему offer до конца древа
  15. while ($z->name === 'offer')
  16. {
  17. $node = new SimpleXMLElement($z->readOuterXML());
  18. $name = $node->name;
  19. $description = $node->description;
  20. $price = $node->price;
  21. $url = $node->url;
  22. $currency = $node->currencyId;
  23. $thumb = $node->thumbnail;
  24. $pictures_array = $node->original_picture; // $pictures_array = $node->picture;
  25. $badchar_fc = array ("\r","\t","\n"); // список плохих символов, которые нужно заменять на пробел.
  26. $description = str_ireplace($badchar_fc, ' ', $description);
  27. $description = preg_replace('! {2,}!u', ' ', $description); // чистка от лишних пробелов
  28. $fp = fopen("txt.txt", "a");
  29. fwrite($fp, "{$name}{$separator}{$description}{$separator}{$pictures_array}{$separator}{$url}\r\n"); // Если в 24 строке заменить на закомментированное, то здесь {$pictures_array[0]}
  30. fclose($fp);
  31.  
  32. // к следующему <offer />
  33. $z->next('offer');
  34. }
Add Comment
Please, Sign In to add comment