473,804 Members | 3,562 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Accessing last table at top level in HTML DOM

I have a HTML page consisting of several tables at the top level of the DOM,
interpersed with other HTML tags. For example:

HTML
BODY
-- SCRIPT
-- SCRIPT
-- #comment
-- SCRIPT
-- A
-- TABLE
-- TABLE
-- TABLE
-- NOSCRIPT
-- #comment
-- BR
-- TABLE
-- #comment
-- #comment

What's the best way of accessing the last table at the top level (i.e. the
last table in the immediate children of HTMLdocument.bo dy)? I'm currently
using the technique shown by the following code snippet, but is there an
easier way?

(ByVal mDoc As mshtml.HTMLDocu ment)

Dim mElems As mshtml.IHTMLEle mentCollection
Dim mElem As mshtml.IHTMLEle ment
Dim mTable As mshtml.IHTMLTab le
Dim i As Integer

mElems = mDoc.body.child ren
For i = mElems.length - 1 To 0 Step -1
mElem = mElems.item(i)
If TypeOf mElem Is mshtml.HTMLTabl eClass Then
mTable = mElem 'the required table - break out here
End If
Next

Any help or info is much appreciated.
Nov 21 '05 #1
3 4273
Hello John,
I have a HTML page consisting of several tables at the top level of
the DOM, interpersed with other HTML tags. For example:

HTML
BODY
What's the best way of accessing the last table at the top level (i.e.
the last table in the immediate children of HTMLdocument.bo dy)? I'm
currently using the technique shown by the following code snippet,
but is there an easier way?

(ByVal mDoc As mshtml.HTMLDocu ment)

Dim mElems As mshtml.IHTMLEle mentCollection
Dim mElem As mshtml.IHTMLEle ment
Dim mTable As mshtml.IHTMLTab le
Dim i As Integer
mElems = mDoc.body.child ren
For i = mElems.length - 1 To 0 Step -1
mElem = mElems.item(i)
If TypeOf mElem Is mshtml.HTMLTabl eClass Then
mTable = mElem 'the required table - break out here
End If
Next
Any help or info is much appreciated.


As far as i'm aware, you're doing alright. You could filter down the list of items you have to go through by requesting all the tables, but this does bring in all the nested tables as well - instead of checking if it's a table, you check the parent to see if it matches the body. Depending on the situation (ie - not many tables in the document), this might end up needing to iterate less often, but there's really no garantees.

Dim mBody as mshtml.IHTMLEle ment = mDoc.body
mElems = mDoc.getElement sByTagName("TAB LE")
for i = mElems.length - 1 to 0 Step -1
mElem = mElems.item(i)
if mElem.parentEle ment.sourceInde x = mBody.sourceInd ex then
mTable = mElem
endif
next

--
Geoff Appleby
http://www.dotnetjunkies.com/weblog/fredd/
Nov 21 '05 #2

"Geoff Appleby" <ge***********@ nospam.gmail.co m> wrote in message
news:%2******** ********@TK2MSF TNGP12.phx.gbl. ..
Hello John,
I have a HTML page consisting of several tables at the top level of
the DOM, interpersed with other HTML tags. For example:

HTML
BODY
What's the best way of accessing the last table at the top level (i.e.
the last table in the immediate children of HTMLdocument.bo dy)? I'm
currently using the technique shown by the following code snippet,
but is there an easier way?

(ByVal mDoc As mshtml.HTMLDocu ment)

Dim mElems As mshtml.IHTMLEle mentCollection
Dim mElem As mshtml.IHTMLEle ment
Dim mTable As mshtml.IHTMLTab le
Dim i As Integer
mElems = mDoc.body.child ren
For i = mElems.length - 1 To 0 Step -1
mElem = mElems.item(i)
If TypeOf mElem Is mshtml.HTMLTabl eClass Then
mTable = mElem 'the required table - break out here
End If
Next
Any help or info is much appreciated.


As far as i'm aware, you're doing alright. You could filter down the list
of items you have to go through by requesting all the tables, but this
does bring in all the nested tables as well - instead of checking if it's
a table, you check the parent to see if it matches the body. Depending on
the situation (ie - not many tables in the document), this might end up
needing to iterate less often, but there's really no garantees.

Dim mBody as mshtml.IHTMLEle ment = mDoc.body
mElems = mDoc.getElement sByTagName("TAB LE")
for i = mElems.length - 1 to 0 Step -1
mElem = mElems.item(i)
if mElem.parentEle ment.sourceInde x = mBody.sourceInd ex then
mTable = mElem
endif
next

--
Geoff Appleby
http://www.dotnetjunkies.com/weblog/fredd/


Thanks Geoff. Yes there are a number of nested tables, I see what you mean.
I've never used the sourceIndex before to match up the element with its
parent, so thanks for showing that too.
Nov 21 '05 #3
Hello John,
Thanks Geoff. Yes there are a number of nested tables, I see what you
mean. I've never used the sourceIndex before to match up the element
with its parent, so thanks for showing that too.


No worries.
Something to be aware of: the sourceindex is an unreliable dynamic value.
By that, what i mean is, the sourceindex is garanteed to be unique, but you can't rely on it being the exact same value whenever the page is reloaded. I any dynamic changes are made to the page (adding or removing elements, for example) then the sourceindexes will go out of whack.
End result: use the sourceindex, but only while the dom has not been modified. Hope that makes sense :)

--
Geoff Appleby
http://www.dotnetjunkies.com/weblog/fredd/
Nov 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2777
by: Paolo Alexis Falcone | last post by:
Whenever I try to access a table with many rows using PgSQL's fetchall(), this happens: >>> from pyPgSQL import PgSQL >>> db = PgSQL.connect("192.168.0.8:5432:whitegold","dondon","dondon") >>> PgSQL.NoPostgresCursor = 1 >>> cur = db.cursor() >>> cur.execute("SELECT * FROM customer") >>> data = cur.fetchall() Traceback (most recent call last):
1
2999
by: Julius Mong | last post by:
Dear all, I have something like this: <html... > <embed ...> </html> Am I out of luck if I wanted to access the embedded DOM and manipulate its content? Or if I have:
1
4340
by: ajay | last post by:
I have following code for a slide menu but i twiked it to work for a single level menu. Open it in a Browser to get a clear picture. I have 2 Qs 1) How to make first entry as non-link. i.e i want to make first text as Table Heading/menu category. For examle in the given menu i want to make a heading as "Comp. Languages" which won't be a link. 2) The position of this menu is absolute to the page. I want to make it absolute to the Table...
1
3142
by: CS Wong | last post by:
Hi, I have a page form where form elements are created dynamically using Javascript instead of programatically at the code-behind level. I have problems accessing the dynamically-created elements and would like to seek a solution for this. I had looked through several articles for accessing programatically-created dynamic elements such as: 1)
4
1605
by: anagai | last post by:
hi I have a javascript function that will add a column of numbers in html table. Is there a way to assign a ID to a group of table rows that so i can easily access the rows through getelementbyid and childnode? I have tried surrounding rows with a tbody tag and assing a id to that. I cant access it through dom. Ive tried surrounding rows with a span tag, with no success. For now i will just have to use the old tbl.rows method to access...
19
10559
by: k.karthikit | last post by:
Hello all, In some hidden variable (<input type="hidden" name="hiddenId" value="test" /> ,i stored some value.I accessed the value "test" using var id = document.getElementById( 'hiddenId' ); It is working fine in IE. But in Mozilla Firefox , null value is returned.Is there any way to use hidden variables compatible to browsers......?
6
4994
by: Ward Germonpré | last post by:
Hi, I have a reference to a dom table. How can I retrieve the number of columns in that table ? The stop value below doesn't work, nor did my experimenting with tbodies and childNodes.. .... var thistable = document.getElementById('resultaattbl'); for (var j=0, stop = thistable.tbody.rows.length; j<stop; j++) {
0
2307
by: Hemant Shah | last post by:
Folks, I have been getting SQLCODE -911 while reading data from a table. When I look at the db2diag.log file I see following: 2006-10-10-14.33.13.384726-240 I127058C470 LEVEL: Warning PID : 233904 TID : 1 PROC : db2agent (CFG) 0 INSTANCE: db2prod NODE : 000 DB : CFG APPHDL : 0-1415 APPID: *LOCAL.db2prod.061010183323 FUNCTION: DB2 UDB, access plan manager,...
8
10847
by: GaryDean | last post by:
I have a Wizard page and need to affect the next and previous buttons from my code-behind. I've googled around and found two solutions, and neither appear to work. I can access the SideBarList steps successfully with the following code... Control myContainer = (Control)Wizard1.FindControl("SideBarContainer"); DataList mySideBarList = (DataList)myContainer.FindControl("SideBarList");
0
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9582
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10580
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10323
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7621
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6854
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5652
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4301
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.