V
V
Vladimir Kokhan2019-03-23 14:48:21
PHP
Vladimir Kokhan, 2019-03-23 14:48:21

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');
        }

The result of the execution of this masterpiece was the receipt of two files. In the first one, 20,000 lines were written; in the second, 20,000 lines were written twice.
The question is - after writing the lines to the first file, you need to delete them from the $data array, and write the rest to the second file. How to do it?
Thanks

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anatoly Medvedev, 2019-03-23
@balamyt92

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.

Do not cut crutches php.net/manual/ru/class.xmlreader.php

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question