I have a come to a scenario whereby I need to overwrite the root (w3c dom) Document element with a new element after it has been created elsewhere. So far I have tried two different ways of achieving this:
document.removeChild(document.getDocumentElement());
AND subsequently this:
newElement = document.getDocumentElement();
newElement = document.createElement("newRootElementName");
document.appendChild(newElement);
Neither seem to overwrite the root element, and, after saving, the document seems only to contain the root element that is empty.
Going with the example I found here, here’s how you could do it. Since there’s apparently no method to change the name of an element, you would have to do the following:
For example: