note 83559 added to domnode.appendchild
| From: | krisdover at hotmail dot com | Date: | Sun, 01 Jun 2008 03:49:02 +0000 |
| Subject: | note 83559 added to domnode.appendchild | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-140038@lists.php.net to get a copy of this message | ||
What's not mentioned here is that DOMNode::appendChild() can also be used to move an existing
node to another part of the DOMDocument, e.g.
<?php
$doc = new DOMDocument();
$doc->loadXML("<foobar><bar/><foo/></foobar>");
$bar = $doc->documentElement->firstChild;
$foo = $doc->documentElement->lastChild;
$foo->appendChild($bar);
print $doc->saveXML();
?>
This produces:
<?xml version="1.0"?>
<foobar><foo><bar/></foo></foobar>
Note that the nodes "<foo/>" and "<bar/>" were siblings, i.e. the
first and last child of "<foobar>" but using appendChild() we were able to move
"<bar/>" so that it is a child of "<foo/>".
This saves you the trouble of doing a DOMNode::removeChild($bar) to remove "<bar/>"
before appending it as a child of "<foo/>".
Kris Dover
----
Server IP: 202.89.58.1
Probable Submitter: 203.97.101.252
----
Manual Page -- http://www.php.net/manual/en/domnode.appendchild.php
Edit -- https://master.php.net/note/edit/83559
Del: integrated -- https://master.php.net/note/delete/83559/integrated
Del: useless -- https://master.php.net/note/delete/83559/useless
Del: bad code -- https://master.php.net/note/delete/83559/bad+code
Del: spam -- https://master.php.net/note/delete/83559/spam
Del: non-english -- https://master.php.net/note/delete/83559/non-english
Del: in docs -- https://master.php.net/note/delete/83559/in+docs
Del: other reasons-- https://master.php.net/note/delete/83559
Reject -- https://master.php.net/note/reject/83559
Search -- https://master.php.net/manage/user-notes.php