DOM Traversal: NodeIterator or TreeWalker?

P

Patient Guy

I posted this question in a moribund group but now post it here:

I have an HTML document with a TABLE element and I want to count the number
of rows, presumably counting TR elements taking into account 'rowspan'
attributes; and the number of columns, presumably counting TH and TD
elements taking into 'colspan' attributes.

I have my own old code (probably bug-ridden) and would rather use the
Traversal DOM.

What is the best to use, a NodeInterator or TreeWalker? The use of the
NodeFilter is a bit confusing. If you have any advice I should consider
when I write this code, thanks.

I want this to work with MSIE too, and I usually write those DOM functions
as best as possible when MSIE has not implemented it. Has MS implemented
it?
 
M

Martin Honnen

Patient Guy wrote:

I have an HTML document with a TABLE element and I want to count the number
of rows, presumably counting TR elements taking into account 'rowspan'
attributes; and the number of columns, presumably counting TH and TD
elements taking into 'colspan' attributes.

The HTML DOM for table elements has a rows collection and each row in
the rows collection has a cells collection.

What is the best to use, a NodeInterator or TreeWalker?

Has MS implemented
it?

No, MSIE does not support anything of DOM Level 2 Traversal.
Mozilla/Firefox has a TreeWalker implementation but no NodeIterator
implementation, Opera 8 and later has both NodeIterator and TreeWalker.
Not sure about Konqueror or Safari, maybe someone else can fill in.
 
M

Matt Kruse

Martin said:
The HTML DOM for table elements has a rows collection and each row in
the rows collection has a cells collection.

However, given rowspan's and colspan's, finding the number of columns from
the .cells.length of the first row, for example, may be incorrect.
One would need to iterate over each cell and increase the count by 1 or the
colspan value.
 
P

Patient Guy

Matt Kruse said:
However, given rowspan's and colspan's, finding the number of columns
from the .cells.length of the first row, for example, may be
incorrect. One would need to iterate over each cell and increase the
count by 1 or the colspan value.

Very helpful information from you and Martin. Thanks for the coding tips.

And I guess I will have to make code for MSIE. A search of the archives
found script for implementing TreeWalker and I will check to see how well
it works.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top