You are viewing a plain text version of this content. The canonical link for it is here.
Posted to batik-users@xmlgraphics.apache.org by fireball <sa...@hotmail.com> on 2012/08/13 21:29:51 UTC

Importing nodes

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
  &lt;g id=&quot;group1&quot;
     ...
     &lt;g id=&quot;group2&quot;
     ...
     &lt;/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-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org


Re: Importing nodes

Posted by fireball <sa...@hotmail.com>.
Thanks Thomas



--
View this message in context: http://batik.2283329.n4.nabble.com/Importing-nodes-tp4655201p4655208.html
Sent from the Batik - Users mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org


Re: Importing nodes

Posted by Thomas DeWeese <th...@gmail.com>.
Sorry send got pressed early.

On Tue, Aug 14, 2012 at 8:50 AM, Thomas DeWeese <th...@gmail.com>wrote:

> 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);
>    current = current.getNextSibling();
> }
>
> On Mon, Aug 13, 2012 at 5:56 PM, jonathan wood <jonathanshawwood@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 <sa...@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-unsubscribe@xmlgraphics.apache.org
>>> For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org
>>>
>>>
>>
>

Re: Importing nodes

Posted by Thomas DeWeese <th...@gmail.com>.
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
<jo...@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 <sa...@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-unsubscribe@xmlgraphics.apache.org
>> For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org
>>
>>
>

Re: Importing nodes

Posted by fireball <sa...@hotmail.com>.
Simple but elegant. 

I still need more time to wrap my head around SVG :-)

Thanks! Jonathan.

fireball.



--
View this message in context: http://batik.2283329.n4.nabble.com/Importing-nodes-tp4655201p4655205.html
Sent from the Batik - Users mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org


Re: Importing nodes

Posted by jonathan wood <jo...@gmail.com>.
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 <sa...@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-unsubscribe@xmlgraphics.apache.org
> For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org
>
>