PHP simplexml_import_dom() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
SimpleXML

Definition

In PHP, simplexml_import_dom() takes a DOMDocument or DOMElement and gives you a SimpleXMLElement for the same XML.

Use it when you already have a DOM tree, for example after loading XML with DOMDocument, and you prefer the shorter SimpleXML syntax such as $xml->title. The two objects share the same XML tree, so a change made through one of them is visible through the other.

Syntax

Call simplexml_import_dom() with the values you need. Start with this line.

PHP
$xml = simplexml_import_dom($dom);
CallWhat it does
simplexml_import_dom($dom)Imports a whole DOMDocument and returns its root element as a SimpleXMLElement.
simplexml_import_dom($dom->documentElement)Imports one DOMElement. The result is the same as importing the document.
simplexml_import_dom($node, 'MyXmlElement')Returns an object of your own class, which must extend SimpleXMLElement.

The function needs both the dom and simplexml extensions, and they are enabled in most PHP installs. The opposite direction is dom_import_simplexml(), which turns a SimpleXMLElement into a DOMElement. Nothing is copied in either direction.

Library declarations
PHP
simplexml_import_dom(SimpleXMLElement|DOMNode $node, ?string $class_name = SimpleXMLElement::class): ?SimpleXMLElement

Parameters

ParameterDescription
nodeRequired. A DOMDocument or a DOMElement. Passing a DOMDocument uses its root element.
class_nameOptional. The class of the returned object. It must be SimpleXMLElement or a class that extends it. The default is SimpleXMLElement.

Return Value

Return ValueDescription
SimpleXMLElementA SimpleXMLElement that points to the imported element.
nullReturns null, with a warning, when the node cannot be used, for example a text node or a DOMDocument that has no root element.

PHP Examples

Example 1 — Import a DOMDocument

Load a fixed XML string with DOMDocument, import it, and read the values with SimpleXML syntax.

PHP
<?php
$dom = new DOMDocument();
$dom->loadXML('<book><title>PHP</title><year>2024</year></book>');

$xml = simplexml_import_dom($dom);

echo 'Root: ', $xml->getName(), PHP_EOL;
echo 'Title: ', $xml->title, PHP_EOL;
echo 'Year: ', $xml->year, PHP_EOL;
echo 'Class: ', get_class($xml), PHP_EOL;

How It Works

  1. A DOMDocument is created and loadXML() reads the fixed XML string into it.
  2. simplexml_import_dom() returns a SimpleXMLElement for the root element, which is book.
  3. $xml->title and $xml->year read the child elements with the short SimpleXML syntax.
  4. get_class() confirms that the result is a SimpleXMLElement object.

Example 2 — Both Objects Share One Tree

Change the XML through SimpleXML and look at it through DOM, then change it through DOM and look at it through SimpleXML.

PHP
<?php
$dom = new DOMDocument();
$dom->loadXML('<book><title>PHP</title></book>');
$xml = simplexml_import_dom($dom);

$xml->title = 'Learn PHP';
echo $dom->saveXML($dom->documentElement), PHP_EOL;

$dom->getElementsByTagName('title')->item(0)->nodeValue = 'PHP 8';
echo $xml->title, PHP_EOL;

How It Works

  1. The DOMDocument is loaded and then imported, so $xml and $dom point to the same XML.
  2. Setting $xml->title changes the title text in the shared tree.
  3. $dom->saveXML($dom->documentElement) prints the root element and shows the new title.
  4. Changing the title with DOM is visible right away when you read $xml->title.

Example 3 — Import One Element

You can also import a single DOMElement instead of the whole document.

PHP
<?php
$dom = new DOMDocument();
$dom->loadXML('<shop><item>Pen</item><item>Book</item></shop>');

$node = $dom->getElementsByTagName('item')->item(1);
$item = simplexml_import_dom($node);
echo $item->getName(), ': ', $item, PHP_EOL;

$shop = simplexml_import_dom($dom);
echo 'Items: ', count($shop->item), PHP_EOL;

How It Works

  1. getElementsByTagName('item')->item(1) selects the second item element, which is Book.
  2. simplexml_import_dom($node) returns a SimpleXMLElement for that single element.
  3. Printing it shows the element name and its text.
  4. Importing the whole document gives access to the root, and count($shop->item) counts both item elements.

Frequently Asked Questions

It converts a DOMDocument or DOMElement into a SimpleXMLElement so you can use the simple $xml->name syntax on an XML tree that was built or loaded with DOM.
No. The SimpleXMLElement and the DOM object share the same tree, so a change through one is visible through the other.
Use dom_import_simplexml(). It takes a SimpleXMLElement and returns a DOMElement that points to the same node.
Both work. If you pass a DOMDocument, PHP uses its root element. If you pass a DOMElement, that element becomes the root of the SimpleXMLElement.
Use it when part of your code already works with DOMDocument but you want easier reading of values. If you start with plain XML text and only need to read it, simplexml_load_string() is simpler.

Next: simplexml_load_file()

simplexml_load_file() reads an XML file and returns a SimpleXMLElement object for its root.

simplexml_load_file() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful