Ok, I did not make myself clear, but your answer already told me that I was
not using DocumentFragment for the right job.
Here is what I meant (I haven't compiled that code, but that is the essence
of what I am doing)
<pre>
javax.xml.parsers.DocumentBuilderFactory dbf=
javax.xml.parsers.DocumentBuilderFactory.newInstance();
javax.xml.parsers.DocumentBuilder db = dbf.newDocumentBuilder();
org.w3c.dom.Document doc1 = db.newDocument();
org.w3c.dom.Document doc2 = db.newDocument();
org.w3c.dom.Node a = doc.createElement("a");
org.w3c.dom.Node c = doc.createElement("c");
org.w3c.dom.Node c1 = doc.createElement("c1");
c.appendChild(c1);
a.appendChild(b); a.appendChild(c);
org.w3c.dom.Node b = doc2.createElement("b");
org.w3c.dom.Node b1 = doc2.createElement("b1");
org.w3c.dom.Node b2 = doc2.createElement("b2");
org.w3c.dom.Node b3 = doc2.createElement("b3");
b.appendChild(b1); b.appendChild(b2); b.appendChild(b3);
org.w3c.dom.DocumentFragment docFrag2 = doc2.createDocumentFragment();
docFrag.appendChild(b1); docFrag.appendChild(b3);
org.w3c.dom.DocumentFragment docFrag1 = doc2.importNode(docFrag2, true);
c.replaceChild(docFrag1, c1);
</pre>
I expected docFrag2 to still contain b1i and b3i (the imported versions of b1 and b3)
After all the references are still valid.
But, for the purpose of this mail I have debugged this piece of code and realised
that adding nodes to the DocumentFragment actually makes it their owner node, which I
didn't know.
I should have know, after all the spec says that a DocumentFragment is just another
Node
I guess I was using DocumentFragment as an easy to use NodeList, which it is not.
Thanks for the answer anyway.
"Swanson, Brion" <[EMAIL PROTECTED]> on 30/08/2001 13:31:54
Please respond to [EMAIL PROTECTED]
To: "'[EMAIL PROTECTED]'" <[EMAIL PROTECTED]>
cc:
Subject: RE: DocumentFragment (bug?)
How did you create your DocumentFragment? From the existing nodes (b1 and
b3)? If so, then I would expect according to the same snippet from the
spec
you have below, that the DocumentFragment you created from those nodes is
not a copy of them, rather a reference. If you insert the DocumentFragment
as a child of another node, then the elements you referenced would move to
that new location.
If you created a DocumentFragment from scratch and made copies of 'b1' and
'b3', then I agree that this seems like an odd behavior.
--------------
Brion Swanson - West Group / Rochester, NY
mailto:[EMAIL PROTECTED]
-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]
Sent: Thursday, August 30, 2001 6:47 AM
To: [EMAIL PROTECTED]
Subject: DocumentFragment (bug?)
Does anyone have a rational explanation why when I replace a node's child
with a DocumentFragment, the later is emptied, or is it a bug?
For example if I have :
<a>
<b>
<b1/>
<b2/>
<b3/>
</b>
<c>
<c1/>
</c>
</a>
and I create a document fragment that contains b1 and b2, and then call
c.replaceChild( docFrag, c1), I get
<a>
<b>
<b2/>
</b>
<c>
<b1/>
<b3/>
</c>
</a>
which is correct. But I would expect the document fragment to still contain
b1 and b2, which it doesn't with Xerces 1.4.3.
Reading the DOM level 2 doc, I can't see a reasonable explanation either,
also it says "various operation --such as inserting nodes as children of
another node-- result in all the node of the docfrag being MOVED to the
child list of the node". But I would understand that as being moved from
<b> to <c>. After all, if I remove the children of a DocumentFragment, they
are not removed from their owner node are they?
Thanks for any answer.
---------------------------------------------------------------------
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]
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]