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