Core\Xml\Reader::read
$reader->read(): ?Core\Xml\NodeReads the next node of an XML document and returns it. At the end of the document, it returns null.
You get a reader from Core\Xml::reader, and you call read in a loop. Each call returns one node: an element, a text, a comment or a processing instruction. An element is returned when its opening tag is read. It has its name and its attributes, but no children. The nodes inside it are returned by the next calls. A closing tag is not a node.
The reader does not build a tree, so it is a good choice for a large document. If the document is broken, read first returns every node before the error, and then throws a ParseError.
The examples below read every node in order, show a document that is broken in the middle, and add up the stock in a product feed.
Return value
Returns ?Core\Xml\Node. The next node: an element, a text, a comment or a processing instruction. The result is null at the end of the document. Every string you read from the node is tainted, because it came from outside the program.
Errors
ParseErrorThe document is not well-formed at the place the reader has reached.Core\Xml::parsethrows the same errors. The nodes before that place were already returned. The reader does not move past the error, so the nextreadthrows the same error again. A document read from bytes also throws this error when its bytes are not UTF-8 or UTF-16, or when it declares another encoding.
Examples
Every node in order
<?nvs// Shows how to read a document one node at a time, from the first node to the last.
Core\Xml\Reader $reader = Core\Xml::reader('<note><to>Ana</to><!--draft--><body>Call me</body></note>');
// `read` returns the next node. At the end of the document it returns `null`, and the loop stops.// This prints six lines, starting with element: note.?Core\Xml\Node $node = $reader->read();while ($node != null) { if ($node->kind() == Core\Xml\Node::Kind::Element) { echo 'element: ', $node->name(), "\n"; } elseif ($node->kind() == Core\Xml\Node::Kind::Text) { echo 'text: ', $node->text(), "\n"; } else { echo 'comment: ', $node->text(), "\n"; } $node = $reader->read();}element: noteelement: totext: Anacomment: draftelement: bodytext: Call meA document broken in the middle
<?nvs// Shows what `read` does when a document is broken in the middle.
// The second `item` is closed by `</list>`, so this document is not well-formed.Core\Xml\Reader $reader = Core\Xml::reader('<list><item>one</item><item>two</list>');
// `read` returns every node before the error. Then it throws a `ParseError`.// This prints list, item, one, item and two, and then the error.try { ?Core\Xml\Node $node = $reader->read(); while ($node != null) { echo $node->name(), $node->text(), "\n"; $node = $reader->read(); }} catch (ParseError $error) { echo 'error: ', $error->message, "\n";}listitemoneitemtwoerror: Core\Xml\Reader::read(): `<item>` is closed by `</list>`, and an element closes as what it opened as.Count the stock in a product feed
<?nvs// Shows how to add up values in a product feed without reading the whole feed into a tree.
string $feed = '<feed> <product sku="A-100" stock="12"/> <product sku="B-200" stock="0"/> <product sku="C-300" stock="7"/></feed>';
Core\Xml\Reader $reader = Core\Xml::reader($feed);int $products = 0;int $stock = 0;int $soldOut = 0;
// The reader does not build a tree. It keeps only the current node and the names of the open elements.?Core\Xml\Node $node = $reader->read();while ($node != null) { if ($node->name() == 'product') { $products = $products + 1; foreach ($node->attributes() as tainted string $name => tainted string $value) { if ($name == 'stock') { // `as ?int` converts the text to a whole number. If that is not possible, the result is `null`. int $count = $value as ?int ?? 0; $stock = $stock + $count; if ($count == 0) { $soldOut = $soldOut + 1; } } } } $node = $reader->read();}
// This prints 3 products, 19 in stock, 1 sold out.echo $products, ' products, ', $stock, ' in stock, ', $soldOut, " sold out\n";3 products, 19 in stock, 1 sold out