simplexml_import_dom()
SimpleXML is a PHP extension that provides a simple and easy-to-use API for working with XML documents. The SimpleXMLElement::importNode() function is one of
Introduction
PHP gives you two distinct ways to handle XML: the DOM extension, which is powerful and standards-compliant but verbose, and SimpleXML, which trades some of that power for a far friendlier, object-property style API. simplexml_import_dom() is the bridge between the two — it takes a node from a DOM tree and hands you back a SimpleXMLElement that wraps the same data.
This is useful when you've done heavy lifting with DOM (validating against a schema, using XPath, manipulating namespaces) but want to read the result with SimpleXML's concise syntax. This page explains how the function works, when to reach for it, and the gotchas that trip people up.
Syntax
simplexml_import_dom(
object $node,
?string $class_name = SimpleXMLElement::class
): SimpleXMLElement|null$node— the DOM node to import. In practice this is aDOMNode,DOMElement, orDOMDocumentinstance. If you pass aDOMDocument, its document element (the root) is imported.$class_name— an optional class that extendsSimpleXMLElement. The returned object is an instance of this class, which lets you attach your own helper methods.
It returns a SimpleXMLElement on success, or null if the node could not be imported. The function has been available since PHP 5.1.3.
Importing a whole document
The most common case is parsing or building a document with DOM and then handing the root over to SimpleXML for easy reading:
<?php
$dom = new DOMDocument();
$dom->loadXML('<?xml version="1.0"?>
<book>
<title>PHP Basics</title>
<author>Jane Doe</author>
</book>');
$book = simplexml_import_dom($dom);
// Read with SimpleXML's property syntax instead of DOM method calls:
echo $book->title; // PHP Basics
echo "\n";
echo $book->author; // Jane Doe
?>Passing the $dom object (a DOMDocument) imports its root element, so $book corresponds to <book>. Compare that to the DOM equivalent — $dom->getElementsByTagName('title')->item(0)->nodeValue — and the appeal of SimpleXML becomes clear.
Importing a single element you built
You can also build a node programmatically with DOM and import just that node:
Here we create a <title> element, attach it to the document, and pass the document's root element ($dom->documentElement) to simplexml_import_dom(). asXML() then serializes the SimpleXMLElement back to a string.
How it relates to the rest of the SimpleXML API
simplexml_import_dom() is the inverse of dom_import_simplexml(), which converts a SimpleXMLElement back into a DOMElement. Together they let you move freely between the two extensions:
| You have | You want | Use |
|---|---|---|
| A DOM node | A SimpleXMLElement | simplexml_import_dom() |
A SimpleXMLElement | A DOMElement | dom_import_simplexml() |
If you're starting from a string or a file rather than a DOM tree, you usually don't need DOM at all — reach for simplexml_load_string() or simplexml_load_file() directly.
Gotchas
- Shared underlying data. The returned
SimpleXMLElementis a view over the same data the DOM node references, not an independent copy. Keep the original DOM object in scope — if it is garbage-collected, the SimpleXML wrapper can become unusable. - Invalid input returns
null. Anullreturn (or, in PHP 8+, aTypeErrorif the argument isn't a DOM object at all) signals failure — always check before chaining method calls. - Custom class must extend
SimpleXMLElement. Passing an unrelated class name for$class_nameis an error; the class has to be a subclass.
Conclusion
simplexml_import_dom() lets you do precise work with the DOM extension and then read or serialize the result with SimpleXML's lightweight syntax. It pairs naturally with dom_import_simplexml() for the round trip back to DOM. When your data already lives in a DOM tree, this function is the cleanest way to keep going with object-property access instead of verbose DOM calls.