Solved

Dynamically Fill Array With Data From XML File

Posted on 2013-11-03
5
464 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

805 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