PHP 5.6.14 is available


(PHP 5)

dom_import_simplexml Gets a DOMElement object from a SimpleXMLElement object


DOMElement dom_import_simplexml ( SimpleXMLElement $node )

This function takes the node node of class SimpleXML and makes it into a DOMElement node. This new object can then be used as a native DOMElement node.



The SimpleXMLElement node.

Return Values

The DOMElement node added or FALSE if any errors occur.


Example #1 Import SimpleXML into DOM with dom_import_simplexml()



if (
$sxe === false) {
'Error while parsing the document';

$dom_sxe dom_import_simplexml($sxe);
if (!
$dom_sxe) {
'Error while converting XML';

$dom = new DOMDocument('1.0');
$dom_sxe $dom->importNode($dom_sxetrue);
$dom_sxe $dom->appendChild($dom_sxe);



See Also

add a note add a note

User Contributed Notes 3 notes

crescentfreshpot at yahoo dot com
7 years ago
justinpatrin at php dot net:
> To get a proper DOM document (which you need to do most things) you need...

No you don't. Just do:
= dom_import_simplexml($xml)->ownerDocument;
Jeff M
6 years ago
SimpleXML is an 'Object Mapping XML API'. It is not DOM, per se. SimpleXML converts the XML elements into PHP's native data types.

The dom_import_simplexml and simplexml_import_dom functions do *not* create separate copies of the original object. You are free to use the methods of either or both interchangeably, since the underlying instance is the same.

// initialize a simplexml object
$sxe = simplexml_load_string('<root/>');
// get a dom interface on the simplexml object
$dom = dom_import_simplexml($sxe);

// dom adds a new element under the root
$element = $dom->appendChild(new DOMElement('dom_element'));
// dom adds an attribute on the new element
$element->setAttribute('creator', 'dom');

// simplexml adds an attribute on the dom element
$sxe->dom_element['sxe_attribute'] = 'added by simplexml';

// simplexml adds a new element under the root
$element = $sxe->addChild('sxe_element');
// simplexml adds an attribute on the new element
$element['creator'] = 'simplexml';

// dom finds the simplexml element (via DOMNodeList->index)
$element = $dom->getElementsByTagName('sxe_element')->item(0);

// dom adds an attribute on the simplexml element
$element->setAttribute('dom_attribute', 'added by dom');
    echo (
    echo (


SimpleXMLElement Object
    [dom_element] => SimpleXMLElement Object
            [@attributes] => Array
                    [creator] => dom
                    [sxe_attribute] => added by simplexml


    [sxe_element] => SimpleXMLElement Object
            [@attributes] => Array
                    [creator] => simplexml
                    [dom_attribute] => added by dom



What this illustrates is that both interfaces are operating on the same underlying object instance. Also, when you dom_import_simplexml, you can create and add new elements without reference to an ownerDocument (or documentElement).

So passing a SimpleXMLElement to another method does not mean the recipient is limited to using SimpleXML methods.

Hey Presto! Your telescope has become a pair of binoculars!
justinpatrin at php dot net
9 years ago
I've found that newer versions of PHP5 require some special syntax in order to properly convert between SimpleXML and DOM. It's not as easy as calling dom_import_simplexml() with a SimpleXML node. To get a proper DOM document (which you need to do most things) you need:
//$xml is a SimpleXML instance
$domnode = dom_import_simplexml($xml);
$dom = new DOMDocument();
$domnode = $dom->importNode($domnode, true);

Switching back, though, is, well...simple.

//$dom is a DOMDocument instance
$xml = simplexml_import_dom($dom);
To Top