Answer the question
In order to leave comments, you need to log in
How to split large xml file?
There is an XML file with about 35,000 entries. You need to upload it to the database. It works on a local server, but not on a real hosting. There is no possibility to change hosting parameters. I decided to split it into several files of 20,000 records, and then upload them to the database one by one. The resulting code is:
$data = new \SimpleXMLElement($request->file('feed'), null, true);
$json = json_encode($data);
$data = json_decode($json);
$strings = count($data->offer); //Общее количество строк (в имеющемся файле их 34.493)
$countString = 20000; // Количество записываемых в файл строк
$countFiles = intval($strings / $countString); // Целочисленное значение количества получаемых файлов
$ostatok = $strings % $countString; // дробное число строк, оставшееся от деления
$xml = new \SimpleXMLElement("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Ads/>");
//Создаем файл
for($key = 0; $key < $countFiles; $key++) {
// Записываем в него данные
for ($i = 0; $i < $countString; $i++) {
$item = $data->offer[$i];
$offer = $xml->addChild('offer');
$offer->addChild('type', $item->type);
}
//Сохраняем на диск
$dom = new \DOMDocument('1.0');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($xml->asXML());
$dom->save('files/feed'.$key.'.xml');
}
Answer the question
In order to leave comments, you need to log in
There is an XML file with about 35,000 entries. It is necessary to fill in it in a DB.... Decided to break into some files on 20.000 records, and then in turn to fill in a DB.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question