Looking for a more elegant way to assign XML node values to an array

simurq

Board Regular
Joined
Nov 11, 2011
Messages
73
Really, I just can't believe that I constantly get the "Can't assign to array" error message... :(
I can read an XML file easily and assign the node values to a collection but the same is not true for an array. I don't know why, I guess I need to read more about Collection vs Array differences but anyway...
I finally came up with a 'dirty' alternative solution using an intermediary variant variable (vID). The code below works OK but... is it somehow possible to do this more elegantly, e.g. without a variant variable (no collections please)?!

Rich (BB code):
Sub ParseXML(sFileName As String)
'Trying to assign XML node values to an array cuz I hate Collections... sometimes!

    Dim xFile As MSXML2.DOMDocument
    Dim xNode As MSXML2.IXMLDOMNode
    Dim xNodes As MSXML2.IXMLDOMNodeList
    Dim vID
    Dim NumberOfNodes As Integer
    Dim i As Integer
    Dim aTemp() As String

    Set xFile = New MSXML2.DOMDocument
    
    xFile.async = False
    xFile.Load (sFileName)
    
    'Start reading XML nodes
    If Not xFile.SelectNodes("//NamedNode") Is Nothing Then
        Set xNodes = xFile.SelectNodes("//NamedNode")
        
        NumberOfNodes = xNodes.Length
        ReDim Preserve aTemp(NumberOfNodes - 1)   'Preparing array for the loop
        
        'This loop is important:
        'i) XML node values are assigned to an intermediary variant vID, then
        'ii) values of vID are assigned to array aTemp()
        For Each xNode In xNodes
            vID = xNode.Attributes.getNamedItem("id").Text
            aTemp(i) = vID
            i = i + 1
        Next
    Else
        Debug.Print "No such nodes..."
    End If
    
    Set xFile = Nothing
'Stop
End Sub

Thanks!
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Please test this, it works for me:

Code:
Sub ParseXML(sFileName$)
Dim xFile As MSXML2.DOMDocument60, xNode As MSXML2.IXMLDOMNode, xNodes As MSXML2.IXMLDOMNodeList, _
i%, aTemp$(), child As MSXML2.IXMLDOMNode, s$
Set xFile = New MSXML2.DOMDocument60
xFile.async = False
xFile.Load (sFileName)
If Not xFile.SelectNodes("//bookstore") Is Nothing Then     ' your node name here
    Set xNodes = xFile.SelectNodes("//bookstore")
    i = 0
    For Each xNode In xNodes
        For Each child In xNode.ChildNodes
            ReDim Preserve aTemp(i)
            aTemp(i) = child.nodeName & ":" & child.Text
            s = s & aTemp(i) & vbLf
            i = i + 1
        Next
    Next
Else
    MsgBox "No such nodes..."
End If
Set xFile = Nothing
MsgBox s
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,821
Messages
6,181,163
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top