treeview|xml| Control | data
Because XML documents are in a tree-structured format, they are usually displayed using the TreeView control. The following code snippet explains how to pass an XML document to a TreeView control in Visual Basic 6.
First, create a new visual Basic 6 project with a form, then add a TreeView control to the form and add a reference to the Microsoft XML parser.
Next, add the following code to the Form_Load event:
Private Sub Form_Load ()
Dim Objdom as DOMDocument
Set objdom = New DOMDocument
Call Objdom.loadxml (_
" ")
Call AddNode (objdom.documentelement, nothing)
Set objdom = Nothing
End Sub
The above code builds and passes in the XML, and then calls the AddNode method. It contains two variables: The added XML node, the TreeView node added. The AddNode method is invoked for the first time, and "Nothing" is passed to the TreeNode variable.
Private Sub AddNode (Byrefxmlnode as IXMLDOMNode, Byreftreenode as Node)
Dim Objnode as Node
Dim XmlNodeList as IXMLDOMNodeList
Dim Lngnodeindex as Long
On Error GoTo Err_handler
If TreeNode is nothing Then
Set Objnode = TreeView1.Nodes.Add
Else
Set objnode = TreeView1.Nodes.Add (TreeNode, Tvwchild)
End If
Objnode.text = Xmlnode.nodename
objnode.expanded = True
' = = ADD child nodes
Set XmlNodeList = Xmlnode.childnodes
For lngnodeindex = 0 to Xmlnodelist.length-1
Call AddNode (Xmlnodelist.item (Lngnodeindex), Objnode)
Next Lngnodeindex
Err_handler:
If err.number <> 0 Then
Msgboxerr.description
End If
End Sub
First, the AddNode method adds a new node to the tree control, and then passes the node's text to the XML node's name. If the XML node contains child nodes, the AddNode method adds the child nodes progressively until all child nodes are passed.