Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7178725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:58:39+00:00 2026-05-28T16:58:39+00:00

I have a come to a scenario whereby I need to overwrite the root

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T16:58:40+00:00Added an answer on May 28, 2026 at 4:58 pm

    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:

    1. Create another element with the new name
    2. Copy the old element’s attributes
    3. Copy the old element’s children
    4. And finally replace the node.

    For example:

    // Obtain the root element
    Element element = document.getDocumentElement();
    
    // Create an element with the new name
    Element element2 = document.createElement("newRootElementName");
    
    // Copy the attributes to the new element
    NamedNodeMap attrs = element.getAttributes();
    for (int i=0; i<attrs.getLength(); i++) {
      Attr attr2 = (Attr)document.importNode(attrs.item(i), true);
      element2.getAttributes().setNamedItem(attr2);
     }
    
    // Move all the children
    while (element.hasChildNodes()) {
      element2.appendChild(element.getFirstChild());
     }
    
    // Replace the old node with the new node
    element.getParentNode().replaceChild(element2, element);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have come across a scenario where we need to track the setting and
I'm brand new to MVC and have just come across a scenario that I
We have come up with a stream strategy which has a main integration stream
I know that my scenario is a little bit twisted. I have come across
Through the years I've come across this scenario more than once. You have a
I am looking into TDD and I have come across a scenario that I
I have a scenario where I need to show a bunch of buttons only
We have a scenario whereby we are hosting an ASP.NET MVC web site on
Tools: SQL2000/5/8, .NET 3.5, C# I have come across an application that has two
My current scenario is that I have 20 Excel files that need to be

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.