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>. .....</bod y></html>.
I can create new tags with Document.createElement(nam e), 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?
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>.
I can create new tags with Document.createElement(nam
How are you suppose to accomplish the adding of surrounding tags to an existing html fragment using w3c dom?
You need to create a new DOM and import any child nodes into it. In your case, set the fragement as a child node
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?
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 use this to make things easier:
http://java.sun.com/javase/6/docs/api/javax/xml/parsers/DocumentBuilder.html#newDocument()
http://java.sun.com/javase/6/docs/api/javax/xml/parsers/DocumentBuilder.html#newDocument()
You can import as many nodes as you want. Once they're imported, you can place them where you want
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
OK - fair enough. Forgot about that sorry