Content · Programming · PHP

PHP Dom\XMLDocument::createFromString() Method: Usage and Examples

Constructs a new PHP 8.4+ DOM document from source markup. 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 Dom\XMLDocument::createFromString is processing data without rebuilding the operation manually. Constructs a new PHP 8.4+ DOM document from source markup.

What Is Dom\XMLDocument::createFromString in PHP?


Constructs a new PHP 8.4+ DOM document from source markup.

Syntax:

Dom\XMLDocument::createFromString($source, $options, $overrideEncoding)

Parameters and Return Value


  • $source — The XML or HTML source string to parse.
  • $options — Bitmask or flags that configure the operation.
  • $overrideEncoding — Explicit encoding override where this modern DOM API supports one.

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

$xmlDocument = Dom\XMLDocument::createFromString("<root><item>Guide</item></root>");
echo $xmlDocument->saveXml();

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

$xmlDocument = Dom\XMLDocument::createFromString("<root><item>Updated guide</item></root>");
echo $xmlDocument->saveXml();

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

$xmlDocument = Dom\XMLDocument::createFromString("<root><item>Inventory item</item></root>");
echo $xmlDocument->saveXml();

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


  • Do not enable external entity loading for untrusted XML. The legacy DOMDocument HTML parser and the modern PHP 8.4 HTML parser follow different HTML parsing rules.
  • 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
Dom\XMLDocument::createFromStringConstructs a new PHP 8.4+ DOM document from source markup.
DOMDocument::loadXMLParses XML markup from a string.

Common Beginner Mistakes


MistakeCorrect Approach
Using this API without checking the required input or extensionDo not enable external entity loading for untrusted XML. The legacy DOMDocument HTML parser and the modern PHP 8.4 HTML parser follow different HTML parsing rules.
Confusing a detached node with one in the documentAppend or import nodes into the intended owner document before querying the tree.
Loading untrusted XML with external entities enabledDisable network entity loading and impose limits on input size.

Key Takeaways


  • Constructs a new PHP 8.4+ DOM document from source markup.
  • Do not enable external entity loading for untrusted XML. The legacy DOMDocument HTML parser and the modern PHP 8.4 HTML parser follow different HTML parsing rules.
  • Test both the expected case and at least one failure or empty-input case before deployment.

Constructs a new PHP 8.4+ DOM document from source markup. Use it when the operation fits your input, output and validation requirements.

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