A common use for DOMDocument::createElement is processing data without rebuilding the operation manually. Creates a new DOM element node owned by the document.
What Is DOMDocument::createElement in PHP?
Creates a new DOM element node owned by the document.
Syntax:
DOMDocument::createElement($localName, $value)
Parameters and Return Value
$localName— The local name of a newly created XML element.$value— The text or setting value.
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 = new DOMDocument("1.0", "UTF-8");
$xmlDocument->loadXML("<catalog><item id=\"one\">First</item></catalog>");
$newElement = $xmlDocument->createElement("item", "Second");
$xmlDocument->documentElement->appendChild($newElement);
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 = new DOMDocument("1.0", "UTF-8");
$xmlDocument->loadXML("<catalog><item id=\"one\">Updated item</item></catalog>");
$newElement = $xmlDocument->createElement("item", "Second");
$xmlDocument->documentElement->appendChild($newElement);
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 = new DOMDocument("1.0", "UTF-8");
$xmlDocument->loadXML("<inventory><item id=\"one\">Inventory</item></inventory>");
$newElement = $xmlDocument->createElement("item", "Second");
$xmlDocument->documentElement->appendChild($newElement);
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 Method | When to Use It |
|---|---|
DOMDocument::createElement | Creates a new DOM element node owned by the document. |
DOMDocument::loadXML | Parses XML markup from a string. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Using this API without checking the required input or extension | 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. |
| Confusing a detached node with one in the document | Append or import nodes into the intended owner document before querying the tree. |
| Loading untrusted XML with external entities enabled | Disable network entity loading and impose limits on input size. |
Key Takeaways
- Creates a new DOM element node owned by the document.
- 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.
Creates a new DOM element node owned by the document. Use it when the operation fits your input, output and validation requirements.