Solved

Dynamically Fill Array With Data From XML File

Posted on 2013-11-03
5
466 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
ID: 39620103
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
ID: 39620372
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
ID: 39620439
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
ID: 39620468
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
ID: 39620750
Great information and guidance. Thank you...
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

860 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