title: How do I copy DOM nodes from one document to another in Java?
body: <p>I'm having trouble with copying nodes from one document to another one. I've used both the adoptNode and importNode methods from Node but they don't work. I've also tried appendChild but that throws an exception. I'm using Xerces. Is this not implemented there? Is there another way to do this?</p>
  
  <pre><code>List&lt;Node&gt; nodesToCopy = ...;
  Document newDoc = ...;
  for(Node n : nodesToCopy) {
      // this doesn't work
      newDoc.adoptChild(n);
      // neither does this
      //newDoc.importNode(n, true);
  }
  </code></pre>