[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Dynamically Fill Array With Data From XML File

Posted on 2013-11-03
5
Medium Priority
?
471 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 30

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 30

Accepted Solution

by:
anarki_jimbel earned 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article will show, step by step, how to integrate R code into a R Sweave document
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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 …
Suggested Courses

650 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