I'm working on a personal project...using Visual Basic 2008 I already
created a tabbed-based web browser and I created the ability to "view
source" and "view source of selected HTML content"...now I'm working
on using the treeview control to represent the HTML DOM - here's what
I have so far for this part...
Private Sub ViewDOMToolStripMenuItem_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
ViewDOMToolStripMenuItem.Click
Dim i As Short
Dim nodeObj As mshtml.IHTMLDOMNode2
'clear the treeview
TreeView1.Nodes.Clear()
'find the <html> tag
For i = 0 To CType(TabControl1.SelectedTab.Controls.Item(0),
WebBrowser).Document.DomDocument.All.length - 1
If CType(TabControl1.SelectedTab.Controls.Item(0),
WebBrowser).Document.DomDocument.All(i).tagName = "HTML" Then
'nodeObj = TreeView1.Nodes.Add(CType
(TabControl1.SelectedTab.Controls.Item(0),
WebBrowser).Document.DomDocument.All.length - 1)
Exit For
End If
Next
'if for some reason we never found an html tag, we stop
parsing altogether
If i = CType(TabControl1.SelectedTab.Controls.Item(0),
WebBrowser).Document.DomDocument.All.length - 1 Then Exit Sub
'MsgBox(numNodes)
Call ParseAndDisplay(CType
(TabControl1.SelectedTab.Controls.Item(0),
WebBrowser).Document.DomDocument.All(i))
'clean up...
nodeObj = Nothing
End Sub
Private Sub ParseAndDisplay(ByRef htmlObj As mshtml.IHTMLElement)
On Error Resume Next
'Dim nodeObj As System.Windows.Forms.TreeNode
Dim nodeObj As mshtml.IHTMLDOMNode2
Dim childObj As mshtml.IHTMLElement
With TreeView1
'if there is no parentelement, add it as the root (html
tag)
If htmlObj.parentElement Is Nothing Then
MessageBox.Show(htmlObj.getElementsByTagName
("body").getAttribute())
'nodeObj = .Nodes.Add("k" & htmlObj.sourceIndex, "<" &
htmlObj.tagName & ">", 0)
nodeObj = .Nodes.Add("k" & htmlObj.sourceIndex,
htmlObj.tagName, 0)
Else
nodeObj = .Nodes.Find("k" &
htmlObj.parentElement.sourceIndex, True)(0).Nodes.Add("k" &
htmlObj.sourceIndex, "<" & htmlObj.tagName & ">", IIf
(htmlObj.children.length = 0, 3, 2))
If htmlObj.children.length = 0 Then
nodeObj = .Nodes.Find("k" & htmlObj.sourceIndex,
True)(0).Nodes.Add("k" & htmlObj.sourceIndex, htmlObj.innerText, 2)
End If
End If
If htmlObj.children.length = 0 Then
Exit Sub 'no children, exit sub
End If
'recursion loop
For Each childObj In htmlObj.children
Call ParseAndDisplay(childObj)
Next childObj
nodeObj.ExpandAll()
End With
nodeObj = Nothing
childObj = Nothing
End Sub
The problem with this code is that it's doesn't quite work, for
instances, it creates the tree however, it only displays the tag name,
I want it to display the tag in its entirety - for example <div
class="..." id=".."> - and it's children nodes
Help me...help me please