This example shows how large XML documents can be read and parsed in
chunks, so that it not necessary to keep the whole document in memory.
Error handling is omitted for brevity.
<?php
$stream = fopen('examples/book-simple.xml', 'r');
$parser = xml_parser_create();
xml_set_element_handler(
$parser,
function($parser, $name, $attributes) { echo $name, PHP_EOL; },
function($parser, $name) { echo $name, PHP_EOL; }
);
while (($data = fread($stream, 16384))) {
xml_parse($parser, $data); // parse the current chunk
}
xml_parse($parser, '', true); // finalize parsing
fclose($stream);