note 89402 added to function.dom-import-simplexml
| From: | JeffM at osu1 dot php dot net | Date: | Fri, 06 Mar 2009 09:09:54 +0000 |
| Subject: | note 89402 added to function.dom-import-simplexml | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-151547@lists.php.net to get a copy of this message | ||
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('<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 ('<pre>');
print_r($sxe);
echo ('</pre>');
?>
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!
----
Server IP: 69.147.83.197
Probable Submitter: 86.129.181.146
----
Manual Page -- http://www.php.net/manual/en/function.dom-import-simplexml.php
Edit -- https://master.php.net/note/edit/89402
Del: integrated -- https://master.php.net/note/delete/89402/integrated
Del: useless -- https://master.php.net/note/delete/89402/useless
Del: bad code -- https://master.php.net/note/delete/89402/bad+code
Del: spam -- https://master.php.net/note/delete/89402/spam
Del: non-english -- https://master.php.net/note/delete/89402/non-english
Del: in docs -- https://master.php.net/note/delete/89402/in+docs
Del: other reasons-- https://master.php.net/note/delete/89402
Reject -- https://master.php.net/note/reject/89402
Search -- https://master.php.net/manage/user-notes.php