I'm getting closer...
I managed to get some of the stuff working now by hacking at it with a big rusty spoon.
However... occasionally doing this
returns null and I can't figure out why. The element is nested in another element - I can see that it has the correct structure by printing the whole document using saveXML().
I've decided to post a function this time because maybe i'm doing something utterly retarded somewhere. The lines which cause problems are colored.
Code: Select all
/* Changes the tagname of a DOMElement node - incomplete */
function changeTagName(&$dom_element, $new_tag_name){
/* Create a new element with the required tag name */
$replacement = $dom_element->ownerDocument->createElement($new_tag_name);
/* Copy all the sub elements */
$children = $dom_element->childNodes;
for($i = 0; $i < $children->length; $i++){
$replacement->appendChild($children->item($i));
}
[color=#FF00FF]$dom_element->parentNode->replaceChild($replacement, $dom_element);[/color]// ParentNode is null
//I also tried doing the following line when parentNode was null (I assumed that would mean it was the root element)
[color=#FF0000]$dom_element->ownerDocument->replaceChild($replacement, $dom_element);[/color]
$dom_element->ownerDocument->saveXML(); // This line prints the whole document how I expect it to look
}
The error i get for the red line is this one: which (apparently) means that the old element is not a (immediate?) child of the node which calls the replace function.
computer wrote:Uncaught exception 'DOMException' with message 'Not Found Error'
Any ideas?

I don't see how both parentNode can be null AND not be an immediate child of the document - ie. a root element.