You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xerces.apache.org by "Singh, Anoop" <an...@hp.com> on 2003/09/30 01:38:53 UTC
Same element append at multiple places in DOM
Hi All,
I am trying to append an element ( I'm calling it common element
) at multiple places in the DOM hierarchy. But it seems
DOM is not accepting the same element at multiple places. In the
output it always has only one common element.
Here is some more detailed analysis what I was trying to do:-
Document doc= new DocumentImpl();
Element root = doc.createElement("set");
Element common = doc.createElement("common"); //This is common
element
Element book2 = doc.createElement("book2");
Element book1 = doc.createElement("book1");
Element document1 = doc.createElement("document");
document1.appendChild( common ); //append common element under
document1
book1.appendChild( common ); //append common element under book1
book1.appendChild( document1 );
root.appendChild( book1 );
root.appendChild( book2 );
doc.appendChild( root );
OUTPUT is:-
<set><book1><common/><document/></book1><book2/></set>
common element is not getting appended to the document1
Is there any such restriction that same element can't be appended at
more than one place ?
I tried by cloning the common element and then appending under document1
, but still no luck.
Then I added one more common element in the end as:-
book2.appendChild( common );
OUTPUT is:-
<set><book1><document/></book1><book2><common/></book2></set>
One interested thing I observed is that it always appends the common
element for the last appendChild() call only!!
Any help or any pointers in this regard will be greatly appreciated.
Thanks,
Anoop Singh
Re: Same element append at multiple places in DOM
Posted by Maksym Kovalenko <mk...@marketswitch.com>.
Here's description of appendChild method as defined in Node interface
(http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030609/core.html#ID-184E7107)
Adds the node newChild to the end of the list of children of this node.
If the newChild is already in the tree, it is first removed.
You might consider using Node.cloneNode() method for cloning your common
node before appending.
Regards,
Max
Singh, Anoop wrote:
> Hi All,
> I am trying to append an element ( I'm calling it common
> element ) at multiple places in the DOM hierarchy. But it seems
> DOM is not accepting the same element at multiple places. In
> the output it always has only one common element.
>
> Here is some more detailed analysis what I was trying to do:-
>
> Document doc= new DocumentImpl();
> Element root = doc.createElement("set");
> Element common = doc.createElement("common"); //This is common element
> Element book2 = doc.createElement("book2");
> Element book1 = doc.createElement("book1");
> Element document1 = doc.createElement("document");
> document1.appendChild( common ); //append common element under
> document1
> book1.appendChild( common ); //append common element under book1
> book1.appendChild( document1 );
> root.appendChild( book1 );
> root.appendChild( book2 );
> doc.appendChild( root );
>
> OUTPUT is:-
> <set><book1><common/><document/></book1><book2/></set>
>
> common element is not getting appended to the document1
> Is there any such restriction that same element can't be appended at
> more than one place ?
> I tried by cloning the common element and then appending under
> document1 , but still no luck.
>
> Then I added one more common element in the end as:-
> book2.appendChild( common );
>
> OUTPUT is:-
> <set><book1><document/></book1><book2><common/></book2></set>
>
> One interested thing I observed is that it always appends the common
> element for the last appendChild() call only!!
>
> Any help or any pointers in this regard will be greatly appreciated.
>
> Thanks,
> Anoop Singh
>
>
--
------------------------------------------------------------------------
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