Skip to content

Instantly share code, notes, and snippets.

@chilismaug
Created May 24, 2019 18:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chilismaug/618055bbe7a952dbcae0f0d26ae9b5dd to your computer and use it in GitHub Desktop.
Save chilismaug/618055bbe7a952dbcae0f0d26ae9b5dd to your computer and use it in GitHub Desktop.
vb easy steps p152 - Reading XML files - done in Excel VBA
VERSION 5.00
Begin {C62A69F0-16DC-11CE-9E98-00AA00574A4F} frmReadXML
Caption = "Read XML Data"
ClientHeight = 4920
ClientLeft = 120
ClientTop = 465
ClientWidth = 9165
OleObjectBlob = "frmReadXML.frx":0000
StartUpPosition = 1 'CenterOwner
End
Attribute VB_Name = "frmReadXML"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Private Sub CommandButton1_Click()
Dim xdocpath As String
Dim oXml As MSXML2.DOMDocument30
Dim eval_set As MSXML2.IXMLDOMNode
Dim eval_prop As MSXML2.IXMLDOMNode
Set oXml = New MSXML2.DOMDocument30
xdocpath = "c:\Temp\dta\books.xml"
oXml.Load (xdocpath)
Debug.Print "loaded oXml - anything? " & oXml.XML
Dim oSeqNodes As IXMLDOMNodeList, oSeqNode As IXMLDOMNode
Set oSeqNodes = oXml.SelectNodes("//shelf/book")
If oSeqNodes.Length = 0 Then
Debug.Print "No see um oSeqNodes xml"
Else
For Each eval_set In oSeqNodes
'.ChildNodes
If eval_set.NodeType = NODE_ELEMENT Then
For Each eval_prop In eval_set.ChildNodes
If eval_prop.NodeType = NODE_ELEMENT Then
MsgBox eval_prop.nodeName & " : " & eval_prop.ChildNodes.Length
Debug.Print eval_prop.nodeName & " : " & eval_prop.Text
End If
Next eval_prop
End If
Next eval_set
End If
Dim counter As Integer: counter = 0
Do Until counter = oSeqNodes.Length
frmReadXML.ListBox1.AddItem (oSeqNodes.Item(counter).SelectSingleNode("title").Text & _
" by " & oSeqNodes.Item(counter).SelectSingleNode("author").Text & vbCrLf)
counter = counter + 1
Loop
End Sub
@chilismaug
Copy link
Author

The data file books.xml was from microsoft rather than mike mcGrath book - it's got a few diff fields but close
https://msdn.microsoft.com/en-us/windows/desktop/ms762271

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment