note 99398 deleted from function.dom-domdocument-renamenode by danbrown
| From: | danbrown@php.net | Date: | Tue, 17 Aug 2010 16:37:36 +0000 |
| Subject: | note 99398 deleted from function.dom-domdocument-renamenode by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-171777@lists.php.net to get a copy of this message | ||
Note Submitter: attila at roughdot dot nl
----
Try this:
/**
* Renames a node in a dom document.
*
* @param DOMElement $node
* @param string $newName
**/
function renameNode(DOMElement $node, $newName)
{
$newNode = $node->ownerDocument->createElement($newName);
foreach ($node->attributes as $attribute) {
$newNode->setAttribute($attribute->nodeName, $attribute->nodeValue);
}
while ($node->firstChild) {
$newNode->appendChild($node->firstChild);
}
$node->ownerDocument->replaceChild($newNode, $node);
}
As suggested by Matthew Turland (http://matthewturland.com/2010/02/09/renaming-a-domnode-in-php/)
If there is a nicer solution, I would love to hear it!