By Brian Yule (byule at PembrokeTechnology.com)
Download the project (40 kb)
That's true, there is MSXML. But sometimes life calls for in-code solutions.
Sometimes you can't wait for MS to fix bugs. Sometimes you need something you can control.
That's where this project comes in. It provides an XML parser with a interface
similar to that of MSXML. The code is light-weight and very quick.
Provided below (it is also in the download) is the code to load any given
XML file into the TreeView control. You can see how remarkably easy the XML parser is to
use.
The code comes as code group containing 2 projects. The 1st one is the XML
Parser implementation itself. It is implemented as an ActiveX DLL. The second project is
the test project that drives the XML Parser and loads a file into the TreeView control. To get
started, simply double-click the g.vbg file. Also included is a sample XML file.
| Add the following to a Form |
Option Explicit
Private Sub btnLoadXmlFile_Click()
Dim oDoc As MYXMLDOM.XMLDOC
'first pick the file
With cd
.Filter = "XML Files (*.xml) |*.xml"
.ShowOpen
If Len(.FileName) = 0 Then Exit Sub
End With
Set oDoc = New MYXMLDOM.XMLDOC
With oDoc
.loadXML (load(cd.FileName))
End With
'load the file into the tree
LoadDocIntoTree oDoc
Set oDoc = Nothing
End Sub
Private Sub LoadDocIntoTree(oDoc As MYXMLDOM.XMLDOC)
Dim oTvNode As MSComctlLib.Node
Dim oXmlNode As MYXMLDOM.XMLNODE
Dim x As Long
tv.Nodes.Clear
With oDoc
'grab the top node - there can only be one
If .HasChildNodes Then
For x = 0 To .ChildNodes.Count - 1
Set oXmlNode = .ChildNodes(x)
If oXmlNode.NodeType <> NODE_COMMENT And oXmlNode.NodeType <> NODE_PROCESSING_INSTRUCTION Then
Set oTvNode = tv.Nodes.Add(, , NextKey(), oXmlNode.NodeName)
oTvNode.EnsureVisible
If oXmlNode.HasChildNodes Then
LoadChildNodesIntoTree oXmlNode, oTvNode
End If
End If
Next
End If
End With
'make sure the top node is visible
If tv.Nodes.Count > 0 Then tv.Nodes(1).EnsureVisible
Set oTvNode = Nothing
Set oXmlNode = Nothing
End Sub
Private Sub LoadChildNodesIntoTree(oXmlNode As XMLNODE, ParentTvNode As MSComctlLib.Node)
Dim x As Long
Dim oChildXmlNode As XMLNODE
Dim oChildTvNode As MSComctlLib.Node
With oXmlNode
If .HasChildNodes Then
For x = 0 To .ChildNodes.Count - 1
Set oChildXmlNode = .ChildNodes(x)
If oChildXmlNode.NodeType <> NODE_COMMENT And oChildXmlNode.NodeType <> NODE_PROCESSING_INSTRUCTION Then
Set oChildTvNode = tv.Nodes.Add(ParentTvNode.Key, tvwChild, NextKey(), oChildXmlNode.NodeName)
oChildTvNode.EnsureVisible
If oChildXmlNode.HasChildNodes Then
LoadChildNodesIntoTree oChildXmlNode, oChildTvNode
End If
End If
Set oChildXmlNode = Nothing
Set oChildTvNode = Nothing
Next
End If
End With
End Sub
'HELPER FUNCTIONS
Private Function load(ByVal FileName As String) As String
Dim xmlStr As String, xmlLine As String
Dim freeFileNo As Integer
freeFileNo = FreeFile
Open FileName For Input As #freeFileNo
If Not EOF(1) Then
Do While Not EOF(freeFileNo)
Input #freeFileNo, xmlLine
xmlStr = xmlStr & " " & xmlLine
Loop
End If
Close #freeFileNo
load = xmlStr
End Function
Private Function NextKey() As String
Static KeyCount As Integer
KeyCount = KeyCount + 1
'"x" is just a dummy letter
'it is not needed here, just my way of doing stuff
NextKey = "x" & KeyCount
End Function
Download the project (40 kb)