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.
<?php // initialize a simplexml object$sxe=simplexml_load_string('');// get a dom interface on the simplexml object$dom=dom_import_simplexml($sxe);// dom adds a new element under the root$element=$dom->appendChild(newDOMElement('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 ('
');print_r($sxe);
echo ('
');?>Outputs:
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!