Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to convert a Node into a Document?

843834May 17 2006 — edited Nov 8 2007
I'm struggling with what I feel should be easy. Basically, I have one DOM that I'm traversing. When I get to a particular Node I'd like to create a separate Document from this Node, so that it's a brand new Document. If I could do that with a particular Nodes' children even better, but for now, stick to the simple example.

Maybe code would help understand what I'm try to do..
// we have some responseElem somewhere in the original document
Node rootOfNewDocument = responseElem.getFirstChild();

// now i want this rootOfNewDocument to be the root of a new Document ... how do i do this?

// this isn't exactly what i want, nor does it work (complains HIERARCHY_REQUEST_ERR: An attempt was 
// made to insert a node where it is not permitted. )
Document responseDoc = builder.newDocument();
Node importedNode = responseDoc.importNode(rootOfNewDocument , true);
responseDoc.appendChild(importedNode);
Is there something simple I'm missing? Can I clone the node and then set it to a Document type somehow? Any help would be must appreciated. Of course, if there's a better place to post a question of this kind, please refer me. Thanks.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 5 2007
Added on May 17 2006
7 comments
552 views