Content · Programming · PHP

PHP XMLReader::next() Method: Usage and Examples

Skips the current node subtree and optionally moves to the next element of a chosen local name. Learn how to apply it with examples and common mistakes.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 Know-Stuff

A common use for XMLReader::next is processing data without rebuilding the operation manually. Skips the current node subtree and optionally moves to the next element of a chosen local name.

What Is XMLReader::next in PHP?


Skips the current node subtree and optionally moves to the next element of a chosen local name.

Syntax:

XMLReader::next($name)

Parameters and Return Value


  • $name — The name of an attribute, element, or node.

Check the returned value, including false when applicable, or handle exceptions for invalid input. The required XML/DOM extension must be installed.

Example 1: Basic Usage


This example demonstrates the main operation with a small, controlled input.

<?php

$xmlReader = new XMLReader();
$xmlReader->XML("<catalog><book id=\"b1\">Guide</book><book id=\"b2\">Reference</book></catalog>");
while ($xmlReader->read() && $xmlReader->name !== "book") {}
$xmlReader->next("book");
echo $xmlReader->getAttribute("id");
$xmlReader->close();

The result depends on the supplied data and the documented operation. Output is not shown as a fixed value when files, locales or runtime configuration can change it.

Example 2: Another Input


This example changes the input or surrounding setup to illustrate another behavior.

<?php

$xmlReader = new XMLReader();
$xmlReader->XML("<catalog><book id=\"b1\">Updated guide</book><book id=\"b2\">Reference</book></catalog>");
while ($xmlReader->read() && $xmlReader->name !== "book") {}
$xmlReader->next("book");
echo $xmlReader->getAttribute("id");
$xmlReader->close();

The result depends on the supplied data and the documented operation. Output is not shown as a fixed value when files, locales or runtime configuration can change it.

Practical Example


This application-style example shows a self-contained usage pattern and checks its result.

<?php

$xmlReader = new XMLReader();
$xmlReader->XML("<inventory><book id=\"b1\">Inventory item</book><book id=\"b2\">Reference</book></inventory>");
while ($xmlReader->read() && $xmlReader->name !== "book") {}
$xmlReader->next("book");
echo $xmlReader->getAttribute("id");
$xmlReader->close();

The result depends on the supplied data and the documented operation. Output is not shown as a fixed value when files, locales or runtime configuration can change it.

Important Behavior and Edge Cases


  • The cursor is forward-only; position it on the intended element before reading attributes or text. Disable network access for untrusted XML, and validate document size.
  • Use the intended input type and handle empty or malformed values explicitly.
  • Where this API requires an extension, version, or file, document that dependency before using it in a real application.

Comparison With Related Functions


Function or MethodWhen to Use It
XMLReader::nextSkips the current node subtree and optionally moves to the next element of a chosen local name.
XMLReader::readAdvances the streaming cursor to the next XML node.

Common Beginner Mistakes


MistakeCorrect Approach
Using this API without checking the required input or extensionThe cursor is forward-only; position it on the intended element before reading attributes or text. Disable network access for untrusted XML, and validate document size.
Reading an attribute before positioning the cursorCall read() until the reader reaches the expected element; only then inspect its attributes.
Reusing a forward-only cursorCreate a new reader or buffer only the selected data when another pass is necessary.

Key Takeaways


  • Skips the current node subtree and optionally moves to the next element of a chosen local name.
  • The cursor is forward-only; position it on the intended element before reading attributes or text. Disable network access for untrusted XML, and validate document size.
  • Test both the expected case and at least one failure or empty-input case before deployment.

Skips the current node subtree and optionally moves to the next element of a chosen local name. Use it when the operation fits your input, output and validation requirements.

Share this resourceSend this page to someone who may find it useful.