Link to home
Start Free TrialLog in
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 CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

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

ASKER

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?
You can import as many nodes as you want. Once they're imported, you can place them where you want
ASKER CERTIFIED SOLUTION
Avatar of bhomass
bhomass

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
OK - fair enough. Forgot about that sorry