note 94153 added to domdocument.importnode
| From: | c dot 1 at smithies dot org | Date: | Tue, 20 Oct 2009 01:40:37 +0000 |
| Subject: | note 94153 added to domdocument.importnode | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-161890@lists.php.net to get a copy of this message | ||
Assume that $source and $dest are instances of DOMDocument. Assume that $sourcedoc contains an
element with ID 'sourceID' and that $destdoc contains an element with ID
'destID'. Assume that we have already set up source and destination element variables
thus:
<?php
$src = $sourcedoc->getElementById('sourceID');
$dst = $destdoc->getElementById('destID');
?>
Finally, assume that $sel has more than one child node.
In order to import the child elements of $src as children of $dst, you might do something like this:
<?php
$src = $dest->importNode($src, TRUE);
foreach ($src->childNodes as $el) $dst->appendChild($el);
?>
But this does not work. foreach gets confused, because (as noted below) appending an imported
element to another existing element in the same document causes it to be removed from its current
parent element.
Therefore, the following technique should be used:
<?php
foreach ($src->childNodes as $el) $dst->appendChild($destdoc->importNode($el, TRUE));
?>
----
Server IP: 92.48.74.199
Probable Submitter: 86.147.23.255
----
Manual Page -- http://www.php.net/manual/en/domdocument.importnode.php
Edit -- https://master.php.net/note/edit/94153
Del: integrated -- https://master.php.net/note/delete/94153/integrated
Del: useless -- https://master.php.net/note/delete/94153/useless
Del: bad code -- https://master.php.net/note/delete/94153/bad+code
Del: spam -- https://master.php.net/note/delete/94153/spam
Del: non-english -- https://master.php.net/note/delete/94153/non-english
Del: in docs -- https://master.php.net/note/delete/94153/in+docs
Del: other reasons-- https://master.php.net/note/delete/94153
Reject -- https://master.php.net/note/reject/94153
Search -- https://master.php.net/manage/user-notes.php