A common use for XMLReader::XML is processing data without rebuilding the operation manually. Loads XML markup from an in-memory string into the streaming reader.
What Is XMLReader::XML in PHP?
Loads XML markup from an in-memory string into the streaming reader.
Syntax:
XMLReader::XML($source, $encoding, $flags)
Parameters and Return Value
$source— The XML or HTML source string to parse.$encoding— The character encoding to apply.$flags— Flags that modify parser behavior.
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("<root><item>Test</item></root>");
var_dump($xmlReader->read());
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("<root><item>Test</item></root>");
var_dump($xmlReader->read());
// Validate the result in a condition appropriate to this operation.
if (function_exists("json_encode")) { echo "Operation demonstrated"; }
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("<root><item>Test</item></root>");
var_dump($xmlReader->read());
// Inspect the surrounding runtime requirements before adapting this example.
echo PHP_VERSION;
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 Method | When to Use It |
|---|---|
XMLReader::XML | Loads XML markup from an in-memory string into the streaming reader. |
XMLReader::read | Advances the streaming cursor to the next XML node. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Using this API without checking the required input or extension | 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. |
| Reading an attribute before positioning the cursor | Call read() until the reader reaches the expected element; only then inspect its attributes. |
| Reusing a forward-only cursor | Create a new reader or buffer only the selected data when another pass is necessary. |
Key Takeaways
- Loads XML markup from an in-memory string into the streaming reader.
- 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.
Loads XML markup from an in-memory string into the streaming reader. Use it when the operation fits your input, output and validation requirements.