Core\Xml::reader
Returns a reader that reads an XML document one node at a time. Each call to read returns the next node, and null at the end of the document.
The document can be a string, bytes, an open file, an uploaded file, Core\Request::bodyStream(), or a list or loop that gives bytes pieces. Pass a file directly. The reader reads it a little at a time, so a document of any size uses the same memory. The reader does not build a tree. It keeps the current node and the names of the elements that are open around it.
An element is returned when its opening tag is read. Its children follow as the next nodes. A closing tag is not a node. depth tells you how many elements are open around the node that read returned last.
The reader finds an error only when it reaches it. The nodes before the error are returned normally, and then read throws a ParseError. The rules are the same as for Core\Xml::parse.
The examples below list the elements of a document, show an error in the middle of one, and count records in a large export file.
Parameters
$documentstring|Core\Bytes::Source|Core\Bytes::ChunksThe document. It can be astring,bytes, an openCore\IO\File, an upload, the request body, or a list or stream ofbyteschunks. The reader reads a file or a stream a little at a time, so a document of any size uses the same memory. Bytes must be UTF-8, or UTF-16 with a byte order mark. Entity references are handled the same way as inCore\Xml::parse.
Return value
Returns Core\Xml\Reader. A reader. Its first read returns the first node of the document.
Errors
RuntimeErrorThe file is already closed.
Examples
List the elements
<?nvs// Shows how to go through an XML document one node at a time and print its elements.
Core\Xml\Reader $reader = Core\Xml::reader('<menu><item>Soup</item><item>Salad</item></menu>');
// `read` returns the next node, and `null` at the end of the document.// `depth` is how many elements are open around that node.while (true) { ?Core\Xml\Node $node = $reader->read(); if ($node == null) { break; } // This prints `menu`, then `item` twice, one step further in. if ($node->kind() == Core\Xml\Node::Kind::Element) { echo Core\Str::repeat(' ', $reader->depth()), $node->name(), "\n"; }}menu item itemAn error in the middle
<?nvs// Shows that the reader finds an error only when it reaches it.
// The second `<entry>` is closed as `</entri>`. Everything before that tag is read without a problem.Core\Xml\Reader $reader = Core\Xml::reader('<log><entry>start</entry><entry>stop</entri></log>');
// `read` throws a `ParseError` at the broken tag. This prints `start` and `stop`, then the error.try { while (true) { ?Core\Xml\Node $node = $reader->read(); if ($node == null) { break; } if ($node->kind() == Core\Xml\Node::Kind::Text) { echo 'text: ', $node->text(), "\n"; } }} catch (ParseError $error) { echo $error->message, "\n";}text: starttext: stopCore\Xml\Reader::read(): `<entry>` is closed by `</entri>`, and an element closes as what it opened as.Count customers in an export
<?nvs// Shows how to count records in a large XML export without building a tree.
// The program writes an export of 10000 customers to a file first, so that it has one to read.array<string> $rows = [];int $i = 0;while ($i < 10000) { string $country = $i % 4 == 0 ? 'AT' : 'DE'; $rows[] = "<customer id=\"" . $i as string . "\" country=\"" . $country . "\"/>"; $i = $i + 1;}string $path = Core\Path::join(Core\IO::createTemporaryDirectory(), 'customers.xml');Core\IO::write($path, '<customers>' . Core\Str::join($rows, '') . '</customers>');
// The reader reads the open file one piece at a time and keeps only the// current node. So the memory it uses stays small for a file of any size.Core\IO\File $file = Core\IO::open($path, Core\IO::FileMode::Read);Core\Xml\Reader $reader = Core\Xml::reader($file);int $austria = 0;while (true) { ?Core\Xml\Node $node = $reader->read(); if ($node == null) { break; } if ($node->name() == 'customer' && $node->attributes()['country'] == 'AT') { $austria = $austria + 1; }}$file->close();
// Every fourth customer is from Austria, so this prints 2500.echo 'customers in AT: ', $austria, "\n";customers in AT: 2500