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.
vadaoAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

aikimarkCommented:
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.
0
Mark WillsTopic AdvisorCommented:
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...
0
ste5anSenior DeveloperCommented:
What have you tried exactly?

Option Compare Database
Option Explicit

Public Sub TestCountGetElementsByTagName()

  'Using reference to MSXML2.
  Dim document As MSXML2.DOMDocument60
  
  Set document = New MSXML2.DOMDocument60
  document.LoadXML "<root><a>1</a><a>2</a><b><a>3</a></b></root>"
  Debug.Print document.getElementsByTagName("a").Length
  Set document = Nothing
   
End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
aikimarkCommented:
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

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
XML

From novice to tech pro — start learning today.