note 72750 modified in function.simplexml-element-addchild by danbrown

From: Date: Fri, 28 Nov 2008 22:32:48 +0000
Subject: note 72750 modified in function.simplexml-element-addchild by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-147354@lists.php.net to get a copy of this message
Rob Richards and I have come up with a very easy way to append one SimpleXML tree to another: <?php function simplexml_append(SimpleXMLElement $parent, SimpleXMLElement $new_child){ $node1 = dom_import_simplexml($parent); $dom_sxe = dom_import_simplexml($new_child); $node2 = $node1->ownerDocument->importNode($dom_sxe, true); $node1->appendChild($node2); } ?> And adding a textnode is also easy: <?php //$sxe1 (refers to SimpleXMLElement) $node1 = dom_import_simplexml($sxe1); $node1->appendChild(new DOMText("my text")); ?> Ofcourse, you could use this to extend the SimpleXMLElement class and overwrite and improve the existing addChild function. Have fun, Luuk Peters. --was-- Rob Richards and I have come up with a very easy way to append one SimpleXML tree to another: <?php function simplexml_append(SimpleXMLElement $parent, SimpleXMLElement $new_child){ $node1 = dom_import_simplexml($parent); $dom_sxe = dom_import_simplexml($new_child); $node2 = $node1->ownerDocument->importNode($dom_sxe, true); $node1->appendChild($node2); } ?> And adding a textnode is also easy: <?php //$sxe1 (refers to SimpleXMLElement) $node1 = dom_import_simplexml($sxe1); $node1->appendChild(new DOMText("my text")); ?> Ofcourse, you could use this to extend the SimpleXMLElement class and overwrite and improve the existing addChild function. Have fun, Luuk Peters. http://php.net/manual/en/function.simplexml-element-addchild.php

« previous php.notes (#147354) next »