getElementsByTagName or childNodes[]

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

getElementsByTagName or childNodes[]

oceatoon
Hi
I'm working on Dom a document,
I am iterating on the markers > marker and every time retreiving the
content of  code and coord,
I would like to know which is the optimal way to call these tags,
through getElementsByTagName by there name or with childNodes[] by there
position ?
Thanks
Tibor

In a structure
<markers>

<marker>
<code></code>
<coord></coord>
</marker>
<marker>
<code></code>
<coord></coord>
</marker>
...
</markers
_______________________________________________
mozilla-dom mailing list
[hidden email]
http://mail.mozilla.org/listinfo/mozilla-dom
Reply | Threaded
Open this post in threaded view
|

Re: getElementsByTagName or childNodes[]

Boris Zbarsky
Tib Kat wrote:
> I would like to know which is the optimal way to call these tags,
> through getElementsByTagName by there name or with childNodes[] by there
> position ?

Not enough information to answer the question.

-Boris
_______________________________________________
mozilla-dom mailing list
[hidden email]
http://mail.mozilla.org/listinfo/mozilla-dom
Reply | Threaded
Open this post in threaded view
|

Re: getElementsByTagName or childNodes[]

Justin Wood (Callek)
Boris Zbarsky wrote:
> Tib Kat wrote:
>> I would like to know which is the optimal way to call these tags,
>> through getElementsByTagName by there name or with childNodes[] by
>> there position ?
>
> Not enough information to answer the question.
>

It probably depends on if you can guarantee the _order_ of those two
tags, and what your goals are.

As, the getElementsByTagName will be more clear when reviewing code
later on and/or skimming it.  And potentially easier to upgrade the
code, should you need to add a new element in the mix. and/or change
order (or make order unimportant).

Given all my points, I personally would go with get*. That is if I do
not need to be as picky about it's performance.

~Justin Wood
_______________________________________________
mozilla-dom mailing list
[hidden email]
http://mail.mozilla.org/listinfo/mozilla-dom
Reply | Threaded
Open this post in threaded view
|

Re: getElementsByTagName or childNodes[]

Boris Zbarsky
Justin Wood (Callek) wrote:
> It probably depends on if you can guarantee the _order_ of those two
> tags

Who said there are two tags?  I see nothing to that effect in the original post...

> As, the getElementsByTagName will be more clear when reviewing code
> later on and/or skimming it.

Really?  Why?

-Boris
_______________________________________________
mozilla-dom mailing list
[hidden email]
http://mail.mozilla.org/listinfo/mozilla-dom
Reply | Threaded
Open this post in threaded view
|

Re: getElementsByTagName or childNodes[]

Martin Honnen-3
In reply to this post by oceatoon


Tib Kat wrote:


> I am iterating on the markers > marker and every time retreiving the
> content of  code and coord,
> I would like to know which is the optimal way to call these tags,
> through getElementsByTagName by there name or with childNodes[] by there
> position ?

> In a structure
> <markers>
>
> <marker>
> <code></code>
> <coord></coord>
> </marker>
> <marker>
> <code></code>
> <coord></coord>
> </marker>
> ...
> </markers

If you are only looking for element nodes and you need to write cross
browser code then I would use getElementsByTagName e.g.
   var markerElements = document.getElementsByTagName('marker');
   for (var i = 0; i < markerElements.length; i++) {
     var markerElement = markerElements[i];
     var code = markerElement.getElementsByTagName('code')[0];
     if (code) {
       // use code here
     }
   }

If you use childNodes then with cross browser script you have to be
aware that white space between element nodes is handled differently by
different implementations, with Mozilla's DOM such a <marker> element
above would have not only the <code> and <coord> element as child nodes
but text nodes with white space too while MSXML used by IE/Win by
default does not have those white space text nodes. That way if you
wanted to use childNodes you could not simply use an index/a position e.g.
   markerElement.childNodes[0]
as in one implementation that could be a text node while in another
implementation that could be an element node.

The only problem using getElementsByTagName as suggested above is that
it gives descendant elements and not only child elements. However with
your example XML that is not a problem as the <marker> elements do only
contain elements as child nodes but there is no deeper nesting.


--

        Martin Honnen
        http://JavaScript.FAQTs.com/
_______________________________________________
mozilla-dom mailing list
[hidden email]
http://mail.mozilla.org/listinfo/mozilla-dom
Reply | Threaded
Open this post in threaded view
|

Re: getElementsByTagName or childNodes[]

Justin Wood (Callek)
In reply to this post by Boris Zbarsky
Boris Zbarsky wrote:
> Justin Wood (Callek) wrote:
>> It probably depends on if you can guarantee the _order_ of those two tags
>
> Who said there are two tags?  I see nothing to that effect in the
> original post...
>

s/tag/element/ I guess <code /> and <coord />

>> As, the getElementsByTagName will be more clear when reviewing code
>> later on and/or skimming it.
>
> Really?  Why?

Instead of using a index-location, it is a specific element-name.  If
the other method is more clear to you, perhaps we have different ways of
thinking about code.  [there are _some_ situations where the childNodes
will be clearer, depending on use-case]

~Justin Wood (Callek)
_______________________________________________
mozilla-dom mailing list
[hidden email]
http://mail.mozilla.org/listinfo/mozilla-dom