VB6Parse an XML Document
' VB client building XML document
   Dim oXML As MSXML.XMLDocument
   Dim oroot As Object
   tvwXML.Nodes.Clear
   Set oXML = New MSXML.XMLDocument
   oXML.URL = txtURL
   Set oroot = oXML.root
   BuildTree oroot, "
   tvwXML.Nodes.Item(1).Expanded = True

Private Sub BuildTree(oElement As IXMLElement, _
   sParentID As String)
Dim sNodeLabel As String
Dim sNodeKey As String
Dim oChild As IXMLElement2
Static iParentID As Integer 
   ' This is used to generate sequential unique IDs. 
   ' First check element type. If it's an element, we  
   ' add it as a node. If it's just text, we add it as
   ' an extension to the parent's text.
If oElement.Type = XMLELEMTYPE_ELEMENT Then
   sNodeLabel = BuildNodeLabel(oElement)
   iParentID = iParentID + 1
   sNodeKey = "Node-" & CStr(iParentID)
   If (Not oElement.Parent Is Nothing) And (sParentID _
      <> ") Then
   tvwXML.Nodes.Add sParentID, tvwChild, sNodeKey, _
      sNodeLabel
   Else
      tvwXML.Nodes.Add , , sNodeKey, sNodeLabel
   End If
   If Not (oElement.children Is Nothing) Then
      For Each oChild In oElement.children
         BuildTree oChild, sNodeKey
      Next oChild
   End If
ElseIf oElement.Type = XMLELEMTYPE_TEXT And sParentID _
   <> " Then
   tvwXML.Nodes(sParentID).Text = tvwXML.Nodes( _
      sParentID).Text + ": " + oElement.Text
End If

End Sub

Listing 3 The Microsoft XML Document object enables you to parse an XML document and display its contents in a treeview. This code shows you how to parse XML on the desktop. Note that you must use a slightly different approach to parse XML in the Web version of the XML viewer. Studying the desktop and Web versions of the viewer can help you understand some of the intrinsic differences between creating traditional VB and VB DHTML apps.