Avatar of vadao
vadao

asked on 

XML Node Counting in MS Access VBA

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.
XMLVBAMicrosoft Access

Avatar of undefined
Last Comment
aikimark
Avatar of aikimark
aikimark
Flag of United States of America image

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.
Avatar of Mark Wills
Mark Wills
Flag of Australia image

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...
ASKER CERTIFIED SOLUTION
Avatar of ste5an
ste5an
Flag of Germany image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of aikimark
aikimark
Flag of United States of America image

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
    Next
    Set oDoc = Nothing
    GetCountOfNodesByTagNameList = lngCount
End Function

Open in new window

Example of invoking the function, looking for nodes "a" and "b" and "root"
?GetCountOfNodesByTagNameList("<root><a>1</a><a>2</a><b><a>3</a></b></root>","a^b^root")
 5 

Open in new window

Microsoft Access
Microsoft Access

Microsoft Access is a rapid application development (RAD) relational database tool. Access can be used for both desktop and web-based applications, and uses VBA (Visual Basic for Applications) as its coding language.

226K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo