PHP Function Reference

PHP simplexml_import_dom() Function



The PHP simplexml_import_dom() function takes a node of a DOM document and converts it into a SimpleXML node. This new object can then be used as a native SimpleXML element.

Syntax

simplexml_import_dom(node, class_name)

Parameters

node Required. Specify a DOM element node.
class_name Optional. Specify the class of the new object. That class should extend the SimpleXMLElement class.

Return Value

Returns a SimpleXMLElement object, or null on failure.

Example: importing DOM

The example below shows the usage of simplexml_import_dom() function.

<?php
$dom = new DOMDocument;
$dom->loadXML('<userlist><user><name>John Smith</name></user></userlist> ');
if (!$dom) {
  echo 'Error while parsing the document';
  exit;
}

$x = simplexml_import_dom($dom);

echo $x->user[0]->name."\n\n";

//displaying the SimpleXMLElement object
print_r($x)
?>

The output of the above code will be:

John Smith

SimpleXMLElement Object
(
    [user] => SimpleXMLElement Object
        (
            [name] => John Smith
        )

)

❮ PHP SimpleXML Reference