Easily read the root, elements (nodes) for children and sibling nodes. Examples show how to read the entire file and search for specific nodes.
Procedure Name | Type | Description |
(Declarations) | Declarations | Declarations and private variables for the CXMLFileRead class. |
FirstChild | Property | Get the FirstChild of the XML file which should show the version info. |
Root | Property | Get the root of the XML file. |
LoadXMLFile | Method | Load the XML file from a physical file on disk. |
LoadXMLString | Method | Load the XML file as a string rather than loading it from a physical file on disk. |
ReadTextFile | Private | Open the named text file and return its contents as a string. |
RootNodeCount | Method | Get the number of nodes on the root. |
ChildNodeCount | Method | Get the number of nodes in the current node. |
GetNextNode | Method | Get the name of the next node. Get all the child nodes, then the sibling nodes, and then back to the parent level. |
GetChildNode | Method | Get the name and value of the specified child node by index. Optionally reset the current node to the new node if it was found. Use the GetChildNodeCount procedure to get the number of child nodes for the current node. |
GetNextChildNode | Method | Get the name and value of the next child node. Optionally reset the current node to the new node if it was found. |
GetNextSiblingNode | Method | Get the name and value of the next sibling node. Optionally resets the current node to the new node if it was found. |
GetChildNodeCount | Method | Get the number of child items in the current node. |
GetSiblingCount | Method | Get the number of siblings for the current node. |
GetNodeValue | Method | Get the node value by calling it explicitly or searching for it. Optionally make it the current node. |
' Example of using the CXMLFileRead class to read XML files in VBA and VB6. ' ' To use this example, create a new module and paste this code into it. ' Then run either of the procedures by putting the cursor in the procedure and pressing: ' F5 to run it, or ' F8 to step through it line-by-line (see the Debug menu for more options) ' View the results in the immediate window Private Const mcstrXMLFile As String = "C:\Total Visual SourceBook 2013\Samples\white-house.xml" Private Sub ReadXMLFileAll() ' Comments: Read all the contents of an XML file or text string using VBA and VB6. ' View results in the immediate window. Dim strError As String Dim lngError As Long Dim strXML As String Dim clsXMLRead As New CXMLFileRead Dim strNodeName As String Dim strValue As String Dim intNodes As Integer If True Then ' If you have an XML file on disk, you can load it like this: strError = clsXMLRead.LoadXMLFile(mcstrXMLFile, lngError) Else ' If you have the file in memory, you can load it without creating a physical file. ' This function can be run if you have the Geospatial module modGoogleMapsAPI loaded 'strXML = GoogleGeocodeXML("White House") strError = clsXMLRead.LoadXMLString(strXML, lngError) End If If strError = "" Then ' If there's no error, you can read the contents of the file Debug.Print "First Child: " & clsXMLRead.FirstChild ' Read the root level values intNodes = clsXMLRead.RootNodeCount Debug.Print "XML Root : " & clsXMLRead.Root Debug.Print "Root Nodes : " & intNodes ' Iterate through all the nodes Do strValue = clsXMLRead.GetNextNode(strNodeName) If (strNodeName <> "") Then intNodes = clsXMLRead.GetChildNodeCount If intNodes = 1 Then Debug.Print "Node : <" & strNodeName & "> " & strValue Else Debug.Print Debug.Print "Node : <" & strNodeName & "> Items: " & intNodes End If End If Loop Until (strNodeName = "") Else Debug.Print "Error : " & strError Debug.Print "Number: " & lngError End If End Sub Private Sub ReadXMLFilePortions() ' Comments: Read and search portions of an XML file or text with VBA and VB6. ' View results in the immediate window. Dim strError As String Dim lngError As Long Dim strXML As String Dim clsXMLRead As New CXMLFileRead Dim strNodeName As String Dim strValue As String Dim intNodes As Integer Dim fFound As Boolean Dim intCount As Integer If True Then ' If you have an XML file on disk, you can load it like this: strError = clsXMLRead.LoadXMLFile(mcstrXMLFile, lngError) Else ' If you have the file in memory, you can load it without creating a physical file. ' This function can be run if you have the Geospatial module modGoogleMapsAPI loaded 'strXML = GoogleGeocodeXML("White House") strError = clsXMLRead.LoadXMLString(strXML, lngError) End If If strError = "" Then Debug.Print "First Child: " & clsXMLRead.FirstChild intNodes = clsXMLRead.RootNodeCount Debug.Print "XML Root : " & clsXMLRead.Root Debug.Print "Root Nodes : " & intNodes ' Get all the child nodes of the current node by referencing it by index Debug.Print Debug.Print "Child Nodes:" For intCount = 1 To intNodes If clsXMLRead.GetChildNode(intCount - 1, False, strNodeName, strValue) Then Debug.Print "Child Node : " & intCount & " <" & strNodeName & "> " & strValue End If Next intCount ' Get a particular node's value without going through the entire XML tree (does not reset the current node) Debug.Print Debug.Print "Specified Nodes:" Debug.Print "Node Latitude : " & clsXMLRead.GetNodeValue("//GeocodeResponse/result/geometry/location/lat", False, fFound) Debug.Print "Node Longitude: " & clsXMLRead.GetNodeValue("//GeocodeResponse/result/geometry/location/lng", False, fFound) ' Search for a particular node Debug.Print Debug.Print "Node Search : " strValue = clsXMLRead.GetNodeValue("//GeocodeResponse/result/address_component/long_name[. ='District of Columbia']", True, fFound) If fFound Then strNodeName = "long_name" Debug.Print "Node : <" & strNodeName & "> " & strValue Debug.Print "Child Nodes: " & clsXMLRead.GetChildNodeCount Debug.Print "Siblings : " & clsXMLRead.GetSiblingCount End If ' Get all the sibling nodes for the current level (does not include the sibling we just retrieved) Do While clsXMLRead.GetNextSiblingNode(True, strNodeName, strValue) Debug.Print "Sibling Node: <" & strNodeName & "> " & strValue Loop ' Get the remainder of the XML file Do strValue = clsXMLRead.GetNextNode(strNodeName) If (strNodeName <> "") Then intNodes = clsXMLRead.GetChildNodeCount If intNodes = 1 Then Debug.Print "Node : <" & strNodeName & "> " & strValue Else Debug.Print Debug.Print "Node : <" & strNodeName & "> Items: " & intNodes End If End If Loop Until (strNodeName = "") Else Debug.Print "Error : " & strError Debug.Print "Number: " & lngError End If End Sub
The source code in Total Visual Sourcebook includes modules and classes for Microsoft Access, Visual Basic 6 (VB6), and Visual Basic for Applications (VBA) developers. Easily add this professionally written, tested, and documented royalty-free code into your applications to simplify your application development efforts.
Total Visual SourceBook is written for the needs of a developer using a source code library covering the many challenges you face. Countless developers over the years have told us they learned some or much of their development skills and tricks from our code. You can too!
Supports Access/Office 2016, 2013, 2010 and 2007, and Visual Basic 6.0!
"The code is exactly how I would like to write code and the algorithms used are very efficient and well-documented."
Van T. Dinh, Microsoft MVP