We help IT Professionals succeed at work.

XML Node Counting in MS Access VBA

vadao asked
Last Modified: 2017-03-22
How would I go about counting the number of nodes for a loaded XML document using MSXML2.DOMDocument60 in MS access VBA without using Xpath? I have tried the getelementsByTagName but I keep getting an error.
Watch Question

aikimarkSocial distance; Wear a mask; Don't touch your face; Wash your hands for 20 seconds
Top Expert 2014

There is a count property on node collections, such as children.  Have you iterated the parents and their children, getting the counts?  This would seem to be the fastest, but depends on your knowledge of the XML document, the document's variability, and depth.

Alternatively, you might iterate every node, keeping a tally of nodes as you traverse the tree.
Mark WillsTopic Advisor, Page Editor
Distinguished Expert 2018

aikimark is correct, you can count nodes...

Something like :
Dim objxmldoc As New MSXML2.DOMDocument60
Dim MyCount As MSXML2.IXMLDOMNodeList

open/load/namespace etc...

Set MyCount = objxmldoc.selectNodes("//ns1:yourtagtocount")

Open in new window

It would help if we could see your code so far...
Senior Developer
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
aikimarkSocial distance; Wear a mask; Don't touch your face; Wash your hands for 20 seconds
Top Expert 2014

Tweaking Ste5an's code...
Public Function GetCountOfNodesByTagNameList(ByVal parmXMLString, ByVal parmNodeTagList, Optional parmDelim = "^") As Long
    Dim oDoc As Object
    Dim vTag As Variant
    Dim lngCount As Long
    Set oDoc = CreateObject("MSXML2.DOMDocument")
    oDoc.LoadXML parmXMLString
    For Each vTag In Split(parmNodeTagList, parmDelim)
        lngCount = lngCount + oDoc.getElementsByTagName(vTag).Length
    Set oDoc = Nothing
    GetCountOfNodesByTagNameList = lngCount
End Function

Open in new window

Example of invoking the function, looking for nodes "a" and "b" and "root"

Open in new window

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions