espositophp
Programmer
- Sep 30, 2003
- 31
Hello, I would like to be able to read from an XML file using pure VB5/VB6 code without making any reference to any external objects.
Now, I have managed to retrieve the data relating to the first occurrence of a single field in the XML document by using the following code:
What I can't do is move on to the second occurrence of the same field in the XML document.
Please note that I'd like to be able to read from a very simple XML file, of which I know the structure. I'm not interested in reading DTDs, schema etc. I don't want to allow the user to modify the XML document. He must only be able to click on a button and move on to the next record.
So, it would be enough to treat it as any other text file, the problem is I got stuck when I tried to implement a recursive search function.
To simplify, my XML document could look like the following:
Can anybody modify the the VB code in Command1_Click in such a way that when I click on it a second time I get the data relating to the second occurrence of <myfield>?
Thanks in advance.
Now, I have managed to retrieve the data relating to the first occurrence of a single field in the XML document by using the following code:
Code:
Private Function InString(SourceLine As String, StartSource As String, EndSource As String)
Dim i As Long
Dim j As Long
Dim TempStr As String
i = Len(StartSource) + InStr(SourceLine, StartSource)
If i > Len(StartSource) Then
j = InStr(i + 1, SourceLine, EndSource)
If j Then
TempStr = Mid$(SourceLine, i + 1, j - i - 1)
InString = TempStr
End If
End If
End Function
Private Sub Command1_Click()
Dim StrXML As String
Dim ff As Long
ff = FreeFile
Open App.Path & "\MyXML.txt" For Input As #ff
StrXML = Input$(LOF(1), 1)
Close #ff
StrXML = InString(StrXML, "<myfield", "</myfield>")
If Len(StrXML) < 3 Then
MsgBox "Invalid data in the field."
Exit Sub
End If
If Len(StrXML) = 0 Then MsgBox "Invalid data in the field."
Text1.Text = StrXML
End Sub
What I can't do is move on to the second occurrence of the same field in the XML document.
Please note that I'd like to be able to read from a very simple XML file, of which I know the structure. I'm not interested in reading DTDs, schema etc. I don't want to allow the user to modify the XML document. He must only be able to click on a button and move on to the next record.
So, it would be enough to treat it as any other text file, the problem is I got stuck when I tried to implement a recursive search function.
To simplify, my XML document could look like the following:
Code:
<?xml version='1.0' encoding='ISO8859-1' ?>
<mydatabase>
<myrecord>
<myfield>This is the first occurrence</myfield>
</myrecord>
<myrecord>
<myfield>This is the second occurrence</myfield>
</myrecord>
</mydatabase>
Can anybody modify the the VB code in Command1_Click in such a way that when I click on it a second time I get the data relating to the second occurrence of <myfield>?
Thanks in advance.