I looked at xmlreader, but, as I see it, using xmlreader, the program will read the file many times (find a question, look for answers, repeat many times) and, therefore, is not viable. I'm wrong?
Yes, you are mistaken. With XMLReader, you specify your own how often you want to go through this file (usually you do it once). In your case, I see no reason why you cannot even insert this 1: 1 into each <row> element. You can choose for the attribute which database (table?) You want to insert.
I usually suggest a set of iterators that make it easy to navigate using XMLReader. It is called XMLReaderIterator and allows foreach on XMLReader to make code often easier to read and write:
$reader = new XMLReader(); $reader->open($xmlFile); $posts = new XMLElementIterator($reader, 'row'); foreach ($posts as $post) { $isAnswerInsteadOfQuestion = (bool)$post->getAttribute('ParentId') $importer = $isAnswerInsteadOfQuestion ? $importerAnswers : $importerQuestions; $importer->importRowNode($post); }
If you are worried about the order (for example, you may be afraid that some of the parent's answers will not be available during the answers), I would take care of it inside the importer layer and not inside the bypass.
Depending on what happens often, very often, never or not at all, I will use a different strategy. For instance. because I would never insert directly into database tables with foreign key constraints enabled. If often, I would create an insert transaction for the entire import, in which the key restrictions were canceled and re-activated at the end.
source share