Solved

Dynamically Fill Array With Data From XML File

Posted on 2013-11-03
5
460 Views
Last Modified: 2013-11-03
Experts, i am using the code below to get some information from an XML file. I need to put this information into an array dynamically. I will never know how many message there are and i will never know how many fields each message will have. Please help...

    Dim xdoc As DOMDocument60
    Set xdoc = New DOMDocument60
    xdoc.validateOnParse = False
   
    xdoc.Load ("C:\test.xml")

    Dim message As IXMLDOMElement
    Dim field As IXMLDOMElement
   
    For Each message In xdoc.selectNodes("/Messages/Message/Name")
       
        Debug.Print message.Text
       
        For Each field In xdoc.selectNodes("/Messages/Message[Name='" & message.Text & "']/Field[Type='Text']/Text")
       
            Debug.Print field.Text
           
        Next
       
    Next
0
Comment
Question by:Basicfarmer
  • 3
  • 2
5 Comments
 

Author Comment

by:Basicfarmer
Comment Utility
I didn't mention that i need the array to be two dimensional. One dimension for the messages and another dimension for the fields each message has.
0
 
LVL 29

Expert Comment

by:anarki_jimbel
Comment Utility
Do you really need to fill an array? I'd use a list of lists or dictionary of lists. This is much more flexible. Something like below:

            Dim dictionaryOfMessages As System.Collections.Generic.Dictionary(Of String, System.Collections.Generic.List(Of String))

            For Each Message As String In xdoc.selectNodes("/Messages/Message/Name")

                Debug.Print(Message.Text)


                Dim fieldList As New System.Collections.Generic.List(Of String)
                dictionaryOfMessages.Add(Message.Text, fieldList)
                For Each field In xdoc.selectNodes("/Messages/Message[Name='" & Message.Text & "']/Field[Type='Text']/Text")

                    Debug.Print(field.Text)
                    fieldList.Add(field.Text)

                Next

            Next

Open in new window


Later if you need you can transfer this data into a 2D array as you will know dimension sizes.
0
 

Author Comment

by:Basicfarmer
Comment Utility
Can you be a little more specific on the use of the dictionary, i dont know how to use one. I need to capture the message names from the xml file and then capture all the fields for each message and store all of that information so i can look up the fields for a given message.
0
 
LVL 29

Accepted Solution

by:
anarki_jimbel earned 500 total points
Comment Utility
Dictionary is one of most useful collections.

I already showed how to add data to a dictionary.

Below code shows how to retrieve data from dictionary:

        ' We have a dictionary:
        Dim dictionaryOfMessages As New System.Collections.Generic.Dictionary(Of String, System.Collections.Generic.List(Of String))
        ' here you fill dictionary from your xml
        ' ... see posted code

        ' Iterate through dictionary to see all entries:
        For Each messageText As String In dictionaryOfMessages.Keys
            System.Diagnostics.Debug.WriteLine("Message = " & messageText)
            System.Diagnostics.Debug.WriteLine("     Fields:")
            Dim fieldsForAMessage As System.Collections.Generic.List(Of String) = _
                    dictionaryOfMessages(messageText)
            For Each fielsText As String In fieldsForAMessage
                System.Diagnostics.Debug.WriteLine("     - " & fielsText)
            Next
        Next

Open in new window



You may also use a bit different approach. You may create a list of lists.
Say, you create a new class, e.g., MessageData, that have two fields: message text and a list of message fields.
For each message read from xml you create an instance of the class. Then you add each message data to the list:

dim messages as new List(of MessageData)

For ....
        messages .Add(MessageDataInstance)

Next
0
 

Author Closing Comment

by:Basicfarmer
Comment Utility
Great information and guidance. Thank you...
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Problem to skip loop 6 48
array6 challenfge 6 62
wordappend challenge 8 81
recursion example 16 65
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now