This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I am trying to Append an new node from one Document to some another document.But appendChild() method was not working. I am getting a runtime exception:- com.sun.xml.tree.DomEx: That node doesn't belong in this document. at com.sun.xml.tree.ParentNode.checkDocument(ParentNode.java:264) at com.sun.xml.tree.ParentNode.appendChild(ParentNode.java:347) I found a solution by using importNode() method which is avialable in JAXP1.1 Is there any solution for above mentioned problem in JAXP1.0??
The DOM specification from W3C states that you can't append nodes from one Document into another. This has to do with - I think - Document security and integrity especially on the web. I tried to do the same once, and I ended up making a little contructing method, that extracted the useful information. This also made it clear to me that there was something conceptually not quite rigth with what I was doing, so maybe there's a reason for the restriction.
Steffen Foldager<p>Sun Certified Java Programmer<br />Sun Certified Web Component Developer
You need to change node owner to the new docment. Refer to API http://www.doc.ic.ac.uk/~sjn5/xml-tr2/docs/api/index.html Steps 1. Read origienal document to which you want to appent node. 2. Read xml document, elements from which are to be appended to the document in step 1 above. Find element to be appended to the document in step 1 above 3. Using document object in step 1 change node owner of the node e.g. xmlDoc.changeNodeOwner(docElement); 4. Now add node in step 3 to element from document in step1 Regards Anup
Have a look at the org.w3c.dom.Document Class's importNode method. This method is designed for this purpose only. The method returns a node which has no parent presently but is owned by the document with whose refrence you would have called the method. After this you can attch this new node to the desired location in your tree.