You are viewing a plain text version of this content. The canonical link for it is here.
Posted to general@xerces.apache.org by Tim O'Donnell <ti...@timodonnell.com> on 2000/02/24 08:00:20 UTC

"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


Re: "Cut & Paste" From DOM to DOM

Posted by Steve Muench <sm...@us.oracle.com>.
Not sure if this is one of those vague details
of the DOM spec that vendors implement differently
without intending to, but with the Oracle XML Parser's
DOM's implementation I just need to first removeChild()
from the "origin" owner document, and then appendChild()
into the new "target".

Failure to first remove the node from the origin doc
results in an exception like "Wrong owner" or something
like that.

Maybe the same trick will work in Xerces.
         
_________________________________________________________
Steve Muench, Consulting Product Manager & XML Evangelist
Business Components for Java Development Team

----- Original Message ----- 
From: "Jeff Mackay" <jm...@vtopia.com>
To: <xe...@xml.apache.org>; <ti...@timodonnell.com>
Sent: Thursday, February 24, 2000 11:20 PM
Subject: RE: "Cut & Paste" From DOM to DOM


| 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:tim@timodonnell.com]
| Sent: Thursday, February 24, 2000 1:00 AM
| To: xerces-dev@xml.apache.org
| 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
| 
| 


RE: "Cut & Paste" From DOM to DOM

Posted by Tim O'Donnell <ti...@timodonnell.com>.
Jeff & Steve,

	A big thanks to you guys for replying to my desparate cry for help =)

	Steve, your idea was one I hadn't thought of.  I tried removeChild, but it
didn't seem to work in this case.  I also thought about trying cloneNode,
but later read that it won't from different docs -- something Jeff pointed
out.

	Jeff, thanks for the explanation of the problem with copying nodes from one
doc to the other.  I had no idea that was the case.  Your suggestions for a
solution were all much appreciated.  I grabbed the importNode method as a
short term solution -- even got it working with Sun's parser.  I think I'm
just going to switch to Xerces since this functionality is apparently built
in, thanks to DOM2.  Besides, I'm using Xalan for XSL, so I might as well be
using Xerces too.

Thanks again for your help guys!!

Tim

-----Original Message-----
From: Jeff Mackay [mailto:jmackay@vtopia.com]
Sent: Thursday, February 24, 2000 11:21 PM
To: xerces-dev@xml.apache.org; tim@timodonnell.com
Subject: RE: "Cut & Paste" From DOM to DOM


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:tim@timodonnell.com]
Sent: Thursday, February 24, 2000 1:00 AM
To: xerces-dev@xml.apache.org
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


RE: "Cut & Paste" From DOM to DOM

Posted by Jeff Mackay <jm...@vtopia.com>.
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:tim@timodonnell.com]
Sent: Thursday, February 24, 2000 1:00 AM
To: xerces-dev@xml.apache.org
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