|
-
May 18th, 2013, 07:43 PM
#1
Thread Starter
New Member
[RESOLVED] Help loading into an arry
Hi folks,
Long time lurker, about 7 years now? This site has been a great amount of help over the years, but I can't find any answers for this one...
Basically, I have a bunch of XML files I have to open, read, parse and assign variables from that XML file to variables in my application.
Each script, for each XML file basically looks like this:
Code:
Dim objNodeList As IXMLDOMNodeList
Dim objNode As IXMLDOMNode
Dim objAttrib As IXMLDOMAttribute
Dim theDoc As MSXML2.DOMDocument
Set theDoc = New MSXML2.DOMDocument
theDoc.Load activeDirectory & "\file1.xml"
location1name = theDoc.selectSingleNode("//location/neighborhood").Text
set theDoc = Nothing
I have to do this for about 5-10 XML files, the amount of XML files changes depending on availability external to the app. Right now I have a logic in place that says if the file exists open it. If the file exists, it sets a flag that says xml1Present = 1
I then have to take another statement that says if xml1Present = 1, open xml 1
THEN once all the variables are assigned I have to use them with the application by individually calling them... To open the variable in the above code I would have to call:
Code:
MsgBox location1Name
Correct me if I'm wrong, but would it not be easier to have all of the load into an array?
ONE XML loading script that basically cycles though all 1 to maybe 15 XML files, then loads the variables (theDoc.selectSingleNode("//location/neighborhood").Text) into an array so that if I wanted to call that data, I'd just have to call:
Code:
MsgBox locationName(0)
MsgBox locationName(0)
' etc?
Thanks again for everyone help over the years.
-
May 18th, 2013, 07:59 PM
#2
Re: Help loading into an arry
First, we will need a peek at your xml file, what is in the file?
Second, yes it is possible to load to a String Array.
Third, if you load 15 files in 1 dimension String Array, it would be easier to load all text from files to a Textbox and using Split() to put whatever data into the String Array.
What do you need in the Array? Do you need the whole file (split line by line) into array or do you need certain information while discarding the <xml> tags (and other tags).
You could also use something like this
Code:
Dim Ele As String 'If not try Object... OR Variant
Dim arrString() As String
Dim i As Integer
Redim arrString(0)
For Each Ele In theDoc
arrString(i) = Ele.Text 'Hopefully it has a text property?
i = i + 1
ReDim Preserve arrString(i)
Next Ele
For i = 0 to ubound(arrString)
debug.Print arrString(i)
next
sorry if there is an error in here, I did not test but it should be very close to what would work (if it doesnt!)
-
May 18th, 2013, 08:43 PM
#3
Thread Starter
New Member
Re: Help loading into an arry
Thanks for the reply.
Not looking to load the whole file, but simply using the XML code above, load variables from the XML file into variables in the VB 6 app.
I've somewhat hacked together what I think the code should look like. Basically open up an XML file, load some variables from that XML document, then be able to call the variables. I hope this hack code explains a bit better what I'm trying to do:
Code:
Dim objNodeList As IXMLDOMNodeList
Dim objNode As IXMLDOMNode
Dim objAttrib As IXMLDOMAttribute
Dim theDoc As MSXML2.DOMDocument
Dim Ele As String 'If not try Object... OR Variant
Dim arrString() As String
Dim i As Integer
ReDim arrString(0)
Set theDoc = New MSXML2.DOMDocument
theDoc.Load "C:\pws\pwsObservation" & (i) & ".xml"
arrString(i) = theDoc.selectSingleNode("//station_id").Text
i = i + 1
ReDim Preserve arrString(i)
For i = 0 To UBound(arrString)
Me.Caption = arrString(0)
Next
-
May 18th, 2013, 09:02 PM
#4
Thread Starter
New Member
Re: Help loading into an arry
*Double post, apologies - didn't see the "must be approved by a moderator" thing last time!*
Just wrote up a post, but it would appear that it didn't take...
Thanks for your reply.
Basically, this is a hack-tastic job of what I'm looking to accomplish:
- Open an XML file, it can be titled pwsObservation0.xml through pwsObservation15.xml and can be called via "C:\pws\pwsObservation" & (i) & ".xml"
- Using theDoc XML code, it will open select variables from the above XML file and load them into an assortment of array variables such as currentTemperature(i) and maybe currentLocation(i), etc
- It will then move on to another XML file and repeat. Assigning each different XML file variables to a new number, right? i.e. currentLocation(0) would refer to the variable loaded in pwsObservation0.xml and currentLocation(1) would refer to the variable loaded in pwsObersvation1.xml, etc etc.
- As mentioned above, I would then call the data by simply calling currentLocation(0), currentLocation(1) etc.
Code:
Dim objNodeList As IXMLDOMNodeList
Dim objNode As IXMLDOMNode
Dim objAttrib As IXMLDOMAttribute
Dim theDoc As MSXML2.DOMDocument
Dim Ele As String 'If not try Object... OR Variant
Dim arrString() As String
Dim i As Integer
ReDim arrString(0)
Set theDoc = New MSXML2.DOMDocument
theDoc.Load "C:\pws\pwsObservation" & (i) & ".xml"
arrString(i) = theDoc.selectSingleNode("//station_id").Text
i = i + 1
ReDim Preserve arrString(i)
For i = 0 To UBound(arrString)
Me.Caption = arrString(0)
Next
This doesn't quite work, but it's my poor mans way of explaining what I'm trying to achieve.
Thanks again!
Last edited by kyle.smith; May 18th, 2013 at 09:33 PM.
-
May 18th, 2013, 10:46 PM
#5
Re: Help loading into an arry
I cannot test at the moment but keep trying... example
Code:
Dim objNodeList As IXMLDOMNodeList
Dim theDoc As MSXML2.DOMDocument
Dim arrString() As String
Dim sText As String
Dim i As Integer
Set theDoc = New MSXML2.DOMDocument
theDoc.Load "C:\pws\pwsObservation" & (i) & ".xml"
'--------------
arrString = theDoc.selectNodes("//station_id")
For i = 0 To UBound(arrString)
sText = sText & arrString(i) & vbNewLine
Next
'--------------
'If above does not work try
'objNodeList = theDoc.selectNodes("//station_id")
'For i = 0 To objNodeList.length - 1 'Might be For i = 1 To objNodeList.length
' sText = sText & objNodeList.Item(i).Text & vbNewLine
'Next
'--------------
MsgBox sText
-
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.
-
May 24th, 2013, 04:17 PM
#7
Thread Starter
New Member
Re: Help loading into an arry
You nailed it! Thank you so much!
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
|