You are viewing a plain text version of this content. The canonical link for it is here.
Posted to xindice-dev@xml.apache.org by Martin Craig <m....@rl.ac.uk> on 2002/10/01 13:15:52 UTC

combining two documents

Hi,

Something similar to this seems to have come up before but I couldn't
make sense of the replies I found in the archives so sorry if I'm
repeating stuff.

I am trying to combine two XMLResources into one document. Naively I
tried:

  XMLResource res = getResource();
  Document doc = getNewDoc();
  Element root = new Element("myroot");
  doc.appendChild(root);

  res_dom = res.getContentAsDOM();
  Node clone = res_dom.cloneNode(true);

  root.appendChild(clone);

However I found that cloneNode returns null :-(

So I tried:

  Node child = res_dom.getFirstChild();
  Node clone = child.cloneNode(true);
  root.appendChild(clone);

Now child is not null. But appendChild gives a wrong document exception.

I don't understand why cloneNode returns null initially. Having got it
to work the second way, I now don't understand why the cloned node
should still belong to the original document!

Could someone point me in the right direction?

Grateful for any help,

Martin.



Re: combining two documents

Posted by Mark Miller <mm...@moondance.com>.
Try Document.importNode() something like this ( I didn't test this, but 
I've used it):

   Node child = res_dom.getFirstChild();
   Node importedNode = root.importNode(child,true);// true == deep
   root.appendChild(importedNode);

On Tuesday, Oct 1, 2002, at 12:15 Europe/Dublin, Martin Craig wrote:

> Hi,
>
> Something similar to this seems to have come up before but I couldn't
> make sense of the replies I found in the archives so sorry if I'm
> repeating stuff.
>
> I am trying to combine two XMLResources into one document. Naively I
> tried:
>
>   XMLResource res = getResource();
>   Document doc = getNewDoc();
>   Element root = new Element("myroot");
>   doc.appendChild(root);
>
>   res_dom = res.getContentAsDOM();
>   Node clone = res_dom.cloneNode(true);
>
>   root.appendChild(clone);
>
> However I found that cloneNode returns null :-(
>
> So I tried:
>
>   Node child = res_dom.getFirstChild();
>   Node clone = child.cloneNode(true);
>   root.appendChild(clone);
>
> Now child is not null. But appendChild gives a wrong document 
> exception.
>
> I don't understand why cloneNode returns null initially. Having got it
> to work the second way, I now don't understand why the cloned node
> should still belong to the original document!
>
> Could someone point me in the right direction?
>
> Grateful for any help,
>
> Martin.
>
>
>