This will work but will be roughly O(N^2) rather than O(N) because the
native data structure for DOM is a linked list of children.
So it is _much_ better to use something like:

Element newParent = doc1.getElementById("group1")
Node current = doc2Elements.getFirstChild();
while (current != NULL) {
   Node copyNode = doc1.importNode(current, true);
   newParent.appendChild(copyNode);

}

I'll also point out that doc1.getElementById("group1") would be best pulled
out of the loop.

On Mon, Aug 13, 2012 at 5:56 PM, jonathan wood
<jonathanshaww...@gmail.com>wrote:

> for (int el = 0; el < numberOfNewElements; el++) {
>     Node anElement = doc2Elements.item(el);
>     Node copyNode = doc1.importNode(anElement, true);
>  //   doc1Root.appendChild(copyNode);
>     doc1.getElementById("group1").appendChild(copyNode);
>
> }
>
> On Mon, Aug 13, 2012 at 3:29 PM, fireball <samiib...@hotmail.com> wrote:
>
>> Let's say I have two documents:
>>
>> document 1:
>> <svg
>>   <g id="group1"
>>      ...
>>   </g>
>> </svg>
>>
>> and
>>
>> document 2:
>> <svg
>>   <g id="group2"
>>      ...
>>   </g>
>> </svg>
>>
>> If I want to import doc 2 into doc 1 I do something like this:
>>
>> Element doc1Root = doc1.getDocumentElement();
>> Element doc2Root = doc2.getDocumentElement();
>>
>> NodeList doc2Elements = doc2Root.getChildNodes();
>> int numberOfNewElements = doc2Elements.getLength();
>> for (int el = 0; el < numberOfNewElements; el++) {
>>     Node anElement = doc2Elements.item(el);
>>     Node copyNode = doc1.importNode(anElement, true);
>>     doc1Root.appendChild(copyNode);
>> }
>>
>>
>> This results in two groups within doc1:
>> <svg
>>   <g id="group1"
>>      ...
>>   </g>
>>   <g id="group2"
>>      ...
>>   </g>
>> </svg>
>>
>> Now my question is, how do we import this doc2 into group1 of doc1. I.e.
>> something like:
>> <svg
>>   <g id="group1"
>>      ...
>>      <g id="group2"
>>      ...
>>      </g>
>>   </g>
>> </svg>
>>
>>
>>
>> --
>> View this message in context:
>> http://batik.2283329.n4.nabble.com/Importing-nodes-tp4655201.html
>> Sent from the Batik - Users mailing list archive at Nabble.com.
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: batik-users-unsubscr...@xmlgraphics.apache.org
>> For additional commands, e-mail: batik-users-h...@xmlgraphics.apache.org
>>
>>
>

Reply via email to