PHP 7.1.0 Release Candidate 4 Released


(PHP 5)

simplexml_import_domGet a SimpleXMLElement object from a DOM node.


SimpleXMLElement simplexml_import_dom ( DOMNode $node [, string $class_name = "SimpleXMLElement" ] )

This function takes a node of a DOM document and makes it into a SimpleXML node. This new object can then be used as a native SimpleXML element.



A DOM Element node


You may use this optional parameter so that simplexml_import_dom() will return an object of the specified class. That class should extend the SimpleXMLElement class.

Zwracane wartości

Returns a SimpleXMLElement lub FALSE w przypadku niepowodzenia.


Przykład #1 Importing DOM

= new DOMDocument;
if (!
$dom) {
'Error while parsing the document';

$s simplexml_import_dom($dom);


Powyższy przykład wyświetli:


Zobacz też:

add a note add a note

User Contributed Notes 1 note

brandonkirsch at perceptionilluminates dot com
3 years ago
simplexml_import_dom() will accept DOMNodes or other SimpleXMLElements.  This has a useful purpose if you are extending the SimpleXMLElement class and would prefer the xpath() method to return your class extension instead of basic SimpleXMLElements.  For example:

class MySimpleXML extends SimpleXMLElement{
    public function xpath($xpath){
        $return = array();
        $simpleXmls = parent::xpath($xpath); // run SimpleXMLElement xpath, returning an array of SimpleXMLElements
        foreach($simpleXmls as $xml){
            $return[] = simplexml_import_dom($xml,'MySimpleXML'); // copy SimpleXMLElement, returning as MySimpleXML instance
        return $return;

Now I can create an instance of MySimpleXML, run an xpath, and get my results back as an array of MySimpleXML elements instead of the native SimpleXMLElement:

$mySimpleXml = new mySimpleXml('<root><node/></root>');

$array = $mySimpleXml->xpath('/root/node'); // returns array with a single MySimpleXml object
To Top