note 59734 added to ref.dom

From: Date: Wed, 14 Dec 2005 14:25:19 +0000
Subject: note 59734 added to ref.dom
Groups: php.notes 
Request: Send a blank email to php-notes+get-100132@lists.php.net to get a copy of this message
Being an experienced ASP developer I was wondering how to replace textual content of a node (with msxml this is simply acheived by setting the 'text' property of a node). Out of frustration I started to play around with SimpleXml but I could not get it to work in combination with xPath. I took me a lot of time to find out so I hope this helps others: function replaceNodeText($objXml, $objNode, $strNewContent){ /* This function replaces a node's string content with strNewContent */ $objNodeListNested = &$objNode->childNodes; foreach ( $objNodeListNested as $objNodeNested ){ if ($objNodeNested->nodeType == XML_TEXT_NODE)$objNode->removeChild ($objNodeNested); } $objNode->appendChild($objXml->createTextNode($strNewContent)); } $objXml= new DOMDocument(); $objXml->loadXML('<root><node id="1">bla</note></root>'); $objXpath = new domxpath($objXml); $strXpath="/root/node[@id='1']"; $objNodeList = $objXpath ->query($strXpath); foreach ($objNodeList as $objNode){ //pass the node by reference replaceNodeText($objXml, &$objNode, $strImportedValue); } ---- Manual Page -- http://www.php.net/manual/en/ref.dom.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+59734 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+59734&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+59734&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+59734&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+59734&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+59734&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+59734&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+59734&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+59734&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#100132) next »