Last active
February 16, 2021 07:31
-
-
Save yos-virtus/98330cbf85bd47cfb0f593ab985690f5 to your computer and use it in GitHub Desktop.
Read large xml files with php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Assuming we have the following xml structure: | |
//<redords> | |
// <record> | |
// <field1>Lorem ipsum</field1> | |
// <field2>Doler</field2> | |
// <field3>Sit amet</field3> | |
// </record> | |
// ... | |
// <record> | |
// <field1>Virescut</field1> | |
// <field2>Volnere</field2> | |
// <field3>Virtus</field3> | |
// </record> | |
//</records> | |
$file = "xml_file_to_read"; | |
$reader = new \XMLReader(); | |
$reader->open($file); | |
while($reader->read()) { | |
if ($reader->nodeType == \XMLReader::ELEMENT and $reader->name == 'record') { | |
$doc = new \DOMDocument('1.0', 'UTF-8'); | |
// Create simple xml object for convinient access to subelements | |
$record = simplexml_import_dom($doc->importNode($reader->expand(), true)); | |
var_dump($record); | |
} | |
} | |
$reader->close(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment