note 45378 added to function.dom-domnode-replacechild

From: Date: Fri, 03 Sep 2004 21:34:50 +0000
Subject: note 45378 added to function.dom-domnode-replacechild
Groups: php.notes 
Request: Send a blank email to php-notes+get-76001@lists.php.net to get a copy of this message
If your XPath query returns a NodeList including a unique item, or if you know for sure the order of the items returned, you can use the "item(n)" syntax instead of the "foreach" syntax. This can greatly improve you code lisibility. As the method name implies, replaceChild cannot replace a node itself but a child of a node. The trick is to use replaceChild on the parent node of your Xpath query result. <?xml version="1.0" ?> <action> <actionGlobal> <actionGlobalFR>sometext</actionGlobalFR> <actionGlobal> </action> $frag = $doc->createElement("actionGlobalFR"); $fragA = $doc->createTextNode("anothertext"); $frag->appendChild($fragA); $xpResult = $xp->query("/action/actionGlobal/actionGlobalFR"); $blipblip = $xpResult->item(0)->parentNode->replaceChild($ajout, $xpResult->item(0)); Et voilà ! Note that the description of replaceChild in the doc is wrong. The correct description is : object DOMNode->replaceChild (object newnode, object oldnode) ---- Manual Page -- http://www.php.net/manual/en/function.dom-domnode-replacechild.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+45378 Delete -- http://master.php.net/manage/user-notes.php?action=delete+45378&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+45378&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#76001) next »