That shouldn't be necessary.  Try this...

Node doc1OwnedNode = doc.importNode(doc2.getDocumentElement(), true);
doc.appendChild(doc1OwnedNode);

Try that. It should work fine. BTW, you aren't importing a "document node". You are importing an Element node and all of its children. The fact that this happens to be the root node of the document is inconsequential. You would be right if the code was doc.importNode(doc2). That wouldn't work.

Jake

At 02:26 PM 11/7/2003 -0800, you wrote:

Thanks everybody.

This method doesn't work. I think because imporNode() can't import a
document node. however, I tried another way,

        Element doc2Root = sourceDoc.getDocumentElement();
        Element docRoot = doc.getDocumentElement();

        for(Node n = sourceRoot; n != null; n=n.getNextSibling()) {
                docRoot.appendChild(doc.importNode(n, true));
        }

which is based on Joe's another email in 2000, it works. There might be
other better way. I will try more and get back later.

Thanks again.

Stephen

On Fri, 7 Nov 2003, Maksym Kovalenko wrote:

> Use
> Node doc1OwnedNode = doc.importNode(doc2.getDocumentElement());
> doc.appendChild(doc1OwnedNode);
>
> But read DOM specs first that Joseph Kesselman pointed out.
>
> Lingzhi Zhang wrote:
>
> >Hi,
> >
> >I am experimenting how to append a piece of xml into an existing xml using
> >DOM. My code is like,
> >
> >     DocumentBuilder parser = factory.newDocumentBuilder();
> >     InputSource in = new InputSource(xmlFile);
> >     Document doc = parser.parse(in);
> >
> >I got anothe Document doc2 in the same way. The I tried
> >
> >1.   doc.appendChild(doc2);
> >2.   doc.appendChild(doc2.getDocumentElement());
> >3.   doc.getDocumentElement().appendChild(doc2.getDocumentElement());
> >4.   aNode.appendChild(doc2);        //aNode is a node in doc
> >
> >But none of the above worked, the code always throws the exception:
> >"org.w3c.dom.DOMException: WRONG_DOCUMENT_ERR: A node is used in a
> >different document than the one that created it."
> >
> >In the api it says,
> >"WRONG_DOCUMENT_ERR: Raised if newChild was created from a different
> >document than the one that created this node. "
> >
> >However, I still don't understand wha this exactly means. What is wrong
> >with the appending in my code?
> >
> >Thanks.
> >
> >Stephen
> >
> >
> >---------------------------------------------------------------------
> >To unsubscribe, e-mail: [EMAIL PROTECTED]
> >For additional commands, e-mail: [EMAIL PROTECTED]
> >
> >
> >
>
> --
> ------------------------------------------------------------------------
>
> Maksym Kovalenko
> Software Engineer
> Marketswitch Corporation
> http://www.marketswitch.com <http://www.marketswitch.com/>
> 108 Powers Court, Suite 225
> Dulles, VA 20166
> Phone: +1 (703) 444-6750 ext. 302
> Fax: +1 (703) 444-6812
>
>


--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to