|
-
May 19th, 2013, 03:18 AM
#6
Re: Help loading into an arry
I know two thirds of nothing about xml but something like this may do what you want
Code:
Option Explicit
Private Type Observation_Data
ObservationNumber As Long
StationID As Long
Temperature As Single
Location As String
'
' add other properties as required
'
End Type
Private theDoc As MSXML2.DOMDocument
Private MyObservations() As Observation_Data
Private Sub cmdGetData_Click()
Dim intObservations As Integer
Dim intThisObs As Integer
Dim intI As Integer
Dim strFile As String
Dim strPrompt As String
strPrompt = InputBox("Enter the Maximum Number of Observations")
If strPrompt <> vbNullString Then
If IsNumeric(strPrompt) Then
intObservations = CInt(strPrompt)
'
' Dimension the Dynamic UDT Array to the largest number of elements expected
'
ReDim MyObservations(intObservations - 1)
intThisObs = 0
'
' Establish the FileName and attempt to Load it
'
For intI = 1 To CInt(strPrompt)
strFile = "C:\pws\pwsObservation" & CStr(intI) & ".xml"
Set theDoc = New MSXML2.DOMDocument
If theDoc.Load(strFile) Then
'
' Loaded OK. Pull out the properties required
' and assign them to the UDT Members appropriately
'
MyObservations(intThisObs).ObservationNumber = intI
MyObservations(intThisObs).StationID = theDoc.selectSingleNode("//station_id").Text
MyObservations(intThisObs).Temperature = theDoc.selectSingleNode("//Temperature").Text
MyObservations(intThisObs).Location = theDoc.selectSingleNode("//Location").Text
'
' etc
'
intThisObs = intThisObs + 1
End If
Next intI
'
' Get rid of any unused elements
'
ReDim Preserve MyObservations(intThisObs - 1)
Else
MsgBox "Please enter a valid integer number greater than zero"
End If
Else
MsgBox "Please enter the maximum number of observations"
End If
End Sub
It will prompt for the maximum number of Observations (i.e. the file the the largest numeric suffix you want to process e.g. entering 15 will set the code to process files with suffixes from 1 to 15 inclusive).
For each of the files (that exist) it will save the required property into the UDT Array to gether with the Observation Number (filename suffix). Thus if you had files 1,2,3,4,5,15
you'd end up with 6 elements in the array with MyObservations(x).ObservationNumber identifying which file the data came from.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|