RE: [PHP] DOM - change a tag name ??
| From: | Marc Christopher Hall | Date: | Wed, 11 Mar 2009 18:07:31 +0000 |
| Subject: | RE: [PHP] DOM - change a tag name ?? | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-289877@lists.php.net to get a copy of this message | ||
Unfortunately, you are correct. This is the only way to do it at the moment.
tagName/nodeName are readonly in the DOM and are set when created. I know
this is possible in the .NET world and I am still beside myself as to which
is the better way.
-----Original Message-----
From: Michael A. Peters [mailto:mpeters@mac.com]
Sent: Wednesday, March 11, 2009 1:40 PM
To: php-general@lists.php.net
Subject: Re: [PHP] DOM - change a tag name ??
Michael A. Peters wrote:
> If I'm manipulating a dom object, is there a way to change the tag name?
> I know you manipulate just about everything else in a node - but is the
> tagName really off limits?
>
> from the documentation for DOMElement -
>
> /* Properties */
> readonly public bool $schemaTypeInfo ;
> readonly public string $tagName ;
>
> so if I really needed to change it, I'd have to create a virgin node
> with the new name, identical attributes and children, and replace the
> existing node with the new one?
>
> Is there any other way to alter the tagName without doing all that?
>
I've not actually tried this (IE may be errors) - I will in a little
bit, but is something like this really the only way to change a tag name?
function changeNodeElement($document,$node,$elementTag) {
$newNode = $document->createElement($elementTag);
// get all attributes from old node
$attributes = $node->attributes;
foreach ($attributes as $attribute) {
$name = $attribute->name;
$value = $attribute->value;
$newNode->setAttribute($name,$value);
}
// get all children from old node
$children = $node->childNodes();
foreach ($children as $child) {
// clone node and add it to newNode
$newChild = $child->cloneNode(true);
$newNode->appendChild($newChild);
}
// replace the old node with the newNode
$node->parent->replaceChild($newNode,$node);
}
It seems that changing the tag name should be a little easier. I don't
expect s/oldtag/newtag/ but am I just missing something obvious?
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
__________ Information from ESET Smart Security, version of virus signature
database 3927 (20090311) __________
The message was checked by ESET Smart Security.
http://www.eset.com
__________ Information from ESET Smart Security, version of virus signature
database 3927 (20090311) __________
The message was checked by ESET Smart Security.
http://www.eset.com