1 Attachment(s)
VB - 3 XML (Or HTML) Tag Reading Functions
Hi All you VB coders out there :D
The three functions that are here are used to extract information from a XML or HTML document.
Also, see the project enclosed for examples of how to use them :)
The first will get the information from between the first set of tags with a specified name it encounters:
VB Code:
Private Function ReturnFirstElementInstance(strDocument As String, strElement As String) As String
Dim strOpenElement As String
Dim strCloseElement As String
Dim intStartPos As Integer
Dim intElementLength As Integer
strOpenElement = "<" & strElement & ">"
strCloseElement = "</" & strElement & ">"
If InStr(strDocument, strOpenElement) > 0 Then
intStartPos = InStr(strDocument, strOpenElement) + Len(strOpenElement)
intElementLength = InStr(strDocument, strCloseElement) - intStartPos
ReturnFirstElementInstance = Mid$(strDocument, intStartPos, intElementLength)
End If
End Function
The call is simple: ReturnFirstElementInstance(strXMLText, strSearchElement)
Where strXMLText is the string containing the XML or HTML text and strSearchElement is a string containing the tag name (No < or >'s) to be searched for.
--------------------------------------------------------------------------
The second will get all the information from between all the tags with a specified name it encounters: (Seperated by the vbNewLine constant)
VB Code:
Private Function ReturnAllElementInstances(strDocument As String, strElement As String) As String
Dim strOpenElement As String
Dim strCloseElement As String
Dim strTemp As String
Dim intStartPos As Integer
Dim intElementLength As Integer
strOpenElement = "<" & strElement & ">"
strCloseElement = "</" & strElement & ">"
If InStr(strDocument, strOpenElement) > 0 Then
While InStr(strDocument, strOpenElement) > 0
intStartPos = InStr(strDocument, strOpenElement) + Len(strOpenElement)
intElementLength = InStr(strDocument$, strCloseElement) - intStartPos
strTemp = Mid$(strDocument$, intStartPos, intElementLength)
strDocument = Right$(strDocument, Len(strDocument) - (intStartPos + intElementLength))
strTemp = strTemp & vbNewLine & strTemp
Wend
ReturnAllElementInstances = strTemp
End If
End Function
The call is simple: ReturnAllElementInstances(strXMLText, strSearchElement)
Where strXMLText is the string containing the XML or HTML text and strSearchElement is a string containing the tag name (No < or >'s) to be searched for.
--------------------------------------------------------------------------
The third will get all the information from a specific tag number (From a given name)
VB Code:
Private Function ReturnSpecificElementInstance(strDocument As String, strElement As String, intInstance As Integer) As String
Dim strOpenElement As String
Dim strCloseElement As String
Dim strTemp As String
Dim intIndex As Integer
Dim intStartPos As Integer
Dim intElementLength As Integer
strOpenElement = "<" & strElement & ">"
strCloseElement = "</" & strElement & ">"
intIndex = 0
If InStr(strDocument, strOpenElement) Then
While InStr(strDocument, strOpenElement) > 0
intIndex = intIndex + 1
intStartPos = InStr(strDocument, strOpenElement) + Len(strOpenElement)
intElementLength = InStr(strDocument, strCloseElement) - intStartPos
strTemp = Mid$(strDocument, intStartPos, intElementLength)
strDocument = Right$(strDocument, Len(strDocument) - (intStartPos + intElementLength))
If intIndex = intInstance Then
ReturnSpecificElementInstance = strTemp
End If
Wend
End If
End Function
The call is simple: ReturnSpecificElementInstance(strXMLText, strSearchElement, intTagNumber)
Where strXMLText is the string containing the XML or HTML text and strSearchElement is a string containing the tag name (No < or >'s) to be searched for. intTagNumber is the tag number to read.
--------------------------------------------------------------------------
I had nothing else to do when I wrote these, it helped me pass the time :)
If anyone else has any other XML / HTML handling functions please post then here :)
Cheers,
RyanJ
Re: VB - 2 XML (Or HTML) tag Reading Functions
This is going to get a lot of use as I've seen many a question on this topic. :thumb:
One question: Why are you using the + sign rather than the & sign for concatenation?
Re: VB - 2 XML (Or HTML) tag Reading Functions
Quote:
Originally Posted by Hack
This is going to get a lot of use as I've seen many a question on this topic. :thumb:
One question: Why are you using the + sign rather than the & sign for concatenation?
OOPs, thats my mistake - I'm trying to convert that to Liberty BASIC also and thats causing my confision.
I will correct those :)
Code has been fixed, attatchment re-uploaded also :)
Also: Thanks for the Rep points Hack, its much appreciated :)
Cheers,
RyanJ
Re: VB - 2 XML (Or HTML) tag Reading Functions
Quote:
Originally Posted by sciguyryan
OOPs, thats my mistake - I'm trying to convert that to Liberty BASIC also and thats causing my confision.
I will correct those :)
Code has been fixed, attatchment re-uploaded also :)
Also: Thanks for the Rep points Hack, its much appreciated :)
Cheers,
RyanJ
Why don't you edit your first post and replace the + signs with the & sign in case someone would prefer to simply copy 'n paste rather than download the sample code. ;)
Re: VB - 2 XML (Or HTML) tag Reading Functions
Quote:
Originally Posted by Hack
Why don't you edit your first post and replace the + signs with the & sign in case someone would prefer to simply copy 'n paste rather than download the sample code. ;)
I already have :)
And congradulations on your 11,000th post :)
Cheers,
RyanJ
Re: VB - 2 XML (Or HTML) tag Reading Functions
Quote:
Originally Posted by sciguyryan
I already have :)
And congradulations on your 11,000th post :)
Cheers,
RyanJ
In both the ReturnAllElementInstances and ReturnAllElementInstances functions, there are two strings called strOpenElement and strCloseElement. I'm pretty certain that you aren't trying to mathmatically add two strings together. :)
PS: Thanks.
Re: VB - 2 XML (Or HTML) tag Reading Functions
Quote:
Originally Posted by Hack
In both the ReturnAllElementInstances and ReturnAllElementInstances functions, there are two strings called strOpenElement and strCloseElement. I'm pretty certain that you aren't trying to mathmatically add two strings together. :)
PS: Thanks.
OOPs, Missed that - Thanks Hack :)
I wonder why it did not pick that up as an error...
Cheers,
RyaNJ
Re: VB - 3 XML (Or HTML) Tag Reading Functions
I've added one more function to the list :)
I've also fixed a big in the other two :)
Cheers,
RyanJ