Computing.Net > Forums > Programming > VB6 and XML

Computing.Net: Over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to sign up now, it's free!

VB6 and XML

Reply to Message Icon

Original Message
Name: Michael
Date: June 16, 2002 at 06:23:55 Pacific
Subject: VB6 and XML
Comment:

Dear Sirs,

Please could someone post a Sub. that will load the XML member data here:

http://www.anarchy-online.com/org/stats/d/2/name/440322/basicstats.xml

to an Array(x,y), where x=member number and y=each of the members details.

I understand this may be asking a fair bit but I am so stuck and do not have a clue about XML.

I look forward to your reply!


Report Offensive Message For Removal


Response Number 1
Name: Jeff J
Date: June 17, 2002 at 14:07:32 Pacific
Reply: (edit)

I was avoiding this, but you got me. Admittedly, despite the supposed simplicity of XML, it's not exactly a learn-in-one-evening affair. I had to learn it on my own before much was out about it, and I've since heard that good tutorials are hard to find. Hey, if anyone knows of one, I'd love to add it to my list.

If you do plan on doing more with XML one day, then I recommend Micro$oft's free XML parser:

"http://msdn.microsoft.com/downloads/default.asp?url=/downloads/topic.asp?URL=/MSDN-FILES/028/000/072/topic.xml"

You can use it within VB like any other COM library, but it is a steep learning curve, so I'll just post a quick-and-dirty parser. If all you need to do is read the xml you posted, then this will do in a pinch:


Option Explicit

'declare a struct
Public Type Member
sFirstName As String
sLastName As String
iRank As Integer
sRankName As String
iLevel As Integer
sProfession As String
sBreed As String
sPhotoUrl As String
End Type


Public Sub LoadXMLData()

Dim sText As String
Dim colMembs As New Collection
Dim Members() As Member
Dim lPos As Long
Dim lEnd As Long
Dim fso As New FileSystemObject
Dim ts As TextStream
Dim i As Integer

Set ts = fso.OpenTextFile("D:\Temp\basicstats.xml", ForReading)
sText = ts.ReadAll
ts.Close

'parse out each member
'inefficient but easy
Do
lPos = InStr(lEnd + 1, sText, "")
If lPos = 0 Then Exit Do
lEnd = InStr(lPos + 9, sText, "
")
If lEnd = 0 Then
Exit Do
Else
lPos = lPos + 8
End If
colMembs.Add Mid$(sText, lPos, lEnd - lPos)
Loop

If colMembs.Count = 0 Then Exit Sub

'1-based to match collections :(
ReDim Members(1 To colMembs.Count)

For i = 1 To colMembs.Count

lPos = InStr(colMembs(i), "")
If lPos > 0 Then
lEnd = InStr(lPos, colMembs(i), "
")
If lEnd > 0 Then
lPos = lPos + 11
Members(i).sFirstName = Mid(colMembs(i), lPos, lEnd - lPos)
End If
End If

lPos = InStr(colMembs(i), "")
If lPos > 0 Then
lEnd = InStr(lPos, colMembs(i), "
")
If lEnd > 0 Then
lPos = lPos + 6
Members(i).iRank = Val(Mid(colMembs(i), lPos, lEnd - lPos))
End If
End If

''''process rest similarly...

Debug.Print Members(i).sFirstName
Debug.Print Members(i).iRank
''..........
Debug.Print vbCrLf
Next
End Sub


You can pop it into any old .bas file. I found it easier to load an array of structs (Types in VB), but you could adapt this to a 2-dimensional array. I made no attempt to download the file, just read it from disk, to save me work. This is lazy code; it won't win any efficiency awards. Nevertheless, remember that XML is case-sensitive, so don't waste parsing time by setting vbTextCompare.

Cheers


Report Offensive Follow Up For Removal

Response Number 2
Name: Jeff J
Date: June 17, 2002 at 14:20:11 Pacific
Reply: (edit)

Foiled again! Let's try this:

Public Sub LoadXMLData()

Dim sText As String
Dim colMembs As New Collection
Dim Members() As Member
Dim lPos As Long
Dim lEnd As Long
Dim fso As New FileSystemObject
Dim ts As TextStream
Dim i As Integer

Set ts = fso.OpenTextFile("D:\Temp\basicstats.xml", ForReading)
sText = ts.ReadAll
ts.Close

'parse out each member
'inefficient but easy
Do
lPos = InStr(lEnd + 1, sText, "[member>")
If lPos = 0 Then Exit Do
lEnd = InStr(lPos + 9, sText, "[/member>")
If lEnd = 0 Then
Exit Do
Else
lPos = lPos + 8
End If
colMembs.Add Mid$(sText, lPos, lEnd - lPos)
Loop

If colMembs.Count [ 1 Then Exit Sub

'1-based to match collections :(
ReDim Members(1 To colMembs.Count)

For i = 1 To colMembs.Count

lPos = InStr(colMembs(i), "[firstname>")
If lPos [> 0 Then
lEnd = InStr(lPos, colMembs(i), "[/firstname>")
If lEnd [> 0 Then
lPos = lPos + 11
Members(i).sFirstName = Mid(colMembs(i), lPos, lEnd - lPos)
End If
End If

lPos = InStr(colMembs(i), "[rank>")
If lPos [> 0 Then
lEnd = InStr(lPos, colMembs(i), "[/rank>")
If lEnd [> 0 Then
lPos = lPos + 6
Members(i).iRank = Val(Mid(colMembs(i), lPos, lEnd - lPos))
End If
End If

''''process rest similarly...

Debug.Print Members(i).sFirstName
Debug.Print Members(i).iRank
''..........
Debug.Print vbCrLf
Next
End Sub


(just replace every "[" with a less-than symbol)


Report Offensive Follow Up For Removal

Response Number 3
Name: Michael
Date: June 18, 2002 at 02:34:08 Pacific
Reply: (edit)

Thankyou Jeff for your time! Looks like I have a fair bit to learn....

Cheers


Report Offensive Follow Up For Removal







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home








Do you have your own blog?

Yes
No
I did before
I will soon


View Results

Poll Finishes In 4 Days.
Discuss in The Lounge
Poll History




Data Recovery Software