Nodes that were originallly part of one document cannot be added to a tree belonging to another document. I believe the reason for this restriction is potential problems with validation (what if the two documents use different DTDs?). So the DocumentFragment won't solve your problem.
There is a Document.importNode method in DOM2 that solves this problem, but I don't think that the Sun implementation offers DOM2 yet. So the solution is to either write the code that duplicates nodes yourself, or switch to an implementation that supports DOM2 (like Xerces). Another option would be to temporarily grab the importNode method from the Xerces DocumentImpl. You might need to modify it somewhat, but it is definitely easier than doing it from scratch. -----Original Message----- From: Tim O'Donnell [mailto:[EMAIL PROTECTED] Sent: Thursday, February 24, 2000 1:00 AM To: [EMAIL PROTECTED] Subject: "Cut & Paste" From DOM to DOM Hi, I have a general XML/DOM question that I've been grappling with all night tonight. It's probably not Xerces specific, so just tell me to take a hike if that's the case. But I'm not sure where else to go... I have several XML documents with the following structure: <Projects> <Project id="<some unique number>"> (more nodes here) </Project> </Projects> My goal is to "merge" all of these documents such that the structure becomes: <Projects> <Project id="<some number>"> ... </Project> <Project id="<some number>"> ... </Project> ... </Projects> In other words, I want all of the "Project" structures to be children of the "Projects" element. So I created a new XmlDocument, created a "Projects" element and appended it to the new document. Now I thought I'd be able to open each file, grab the "Project" node, and append it to my "Projects" node. Apparently I was wrong. My friendly JRE informs me that: com.sun.xml.tree.DomEx: That node doesn't belong in this document. Not quite sure what exactly that error means. I tried to grab the entire document this time and append it to my new XmlDocument. Still no luck. I'm really stuck as to how I go about "cutting & pasting" the Project node from one XML document, and appending it to another tree. My guts tell me I need to use a DocumentFragment, thus preserving all of the children of the Project nodes. But the only method I see in the DOM spec which uses a DocumentFragment is for the XmlDocument class (createDocumentFragment). However, that method only creates an empty DocumentFragment. I'm terrified that I might have to create an empty DocumentFragment and somehow recursively add all of the children nodes, AND THEN append the whole DocumentFragment to my XmlDocument. So here I am. Confused (what does that error message really mean?), weary (I've tried almost everything I can think of, and seen that error so many times I just may cry myself to sleep tonight), and scared (that I may have to write the recursive routine to build the docfrag). I guess I just want someone to point me in the right direction. Am I correct in my docfrag thoughts? Or is there a better/simpler way to accomplish this task? I have to imagine this is a rather common task in XML, so there has to be a simple way to do it. Thanks so much for any help you guys can provide! In the immortal words of our favorite princess: "Help me Xerces-dev. You're my only hope!" -Tim
