Avatar of bhomass
bhomass asked on

change the root (Element)node of a w3c dom document

I am in the process of changing a dom4j implementation to a the w3c dom. I am not able to find many of the equivalent calls.

for example, there is a case when I want to take a html fragment which starts with a <div>, and add the surrounding html tags, starting with <html><head></head><body>......</body></html>.

I can create new tags with Document.createElement(name), but I don't see any way to set the new <html> tag as the root element, nor any way to change the parent node of the <div> tag. In fact, it would be nice if I can change the <div> tag to a <body> tag. but I don't see any api to change tag name either.

How are you suppose to accomplish the adding of surrounding tags to an existing html fragment using w3c dom?

Avatar of undefined
Last Comment

8/22/2022 - Mon

You need to create a new DOM and import any child nodes into it. In your case, set the fragement as a child node

you are being too quick.

first of all I don't see any constructor for Document. I don't know any way to create this new DOM without it coming from the parser.

second, the document has an import method, but only for one external node into the importing document. I assume it is an empty document. How do I add the surrounding tags before importing the external element under them?

Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes

You can import as many nodes as you want. Once they're imported, you can place them where you want

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question

OK - fair enough. Forgot about that sorry