Results 1 to 8 of 8

Thread: VB - 3 XML (Or HTML) Tag Reading Functions

Threaded View

  1. #1

    Thread Starter
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    VB - 3 XML (Or HTML) Tag Reading Functions

    Hi All you VB coders out there

    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:
    1. Private Function ReturnFirstElementInstance(strDocument As String, strElement As String) As String
    2.     Dim strOpenElement As String
    3.     Dim strCloseElement As String
    4.     Dim intStartPos As Integer
    5.     Dim intElementLength As Integer
    6.     strOpenElement = "<" & strElement & ">"
    7.     strCloseElement = "</" & strElement & ">"
    8.     If InStr(strDocument, strOpenElement) > 0 Then
    9.         intStartPos = InStr(strDocument, strOpenElement) + Len(strOpenElement)
    10.         intElementLength = InStr(strDocument, strCloseElement) - intStartPos
    11.         ReturnFirstElementInstance = Mid$(strDocument, intStartPos, intElementLength)
    12.     End If
    13. 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:
    1. Private Function ReturnAllElementInstances(strDocument As String, strElement As String) As String
    2.     Dim strOpenElement As String
    3.     Dim strCloseElement As String
    4.     Dim strTemp As String
    5.     Dim intStartPos As Integer
    6.     Dim intElementLength As Integer
    7.     strOpenElement = "<" & strElement & ">"
    8.     strCloseElement = "</" & strElement & ">"
    9.     If InStr(strDocument, strOpenElement) > 0 Then
    10.         While InStr(strDocument, strOpenElement) > 0
    11.             intStartPos = InStr(strDocument, strOpenElement) + Len(strOpenElement)
    12.             intElementLength = InStr(strDocument$, strCloseElement) - intStartPos
    13.             strTemp = Mid$(strDocument$, intStartPos, intElementLength)
    14.             strDocument = Right$(strDocument, Len(strDocument) - (intStartPos + intElementLength))
    15.             strTemp = strTemp & vbNewLine & strTemp
    16.         Wend
    17.         ReturnAllElementInstances = strTemp
    18.     End If
    19. 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:
    1. Private Function ReturnSpecificElementInstance(strDocument As String, strElement As String, intInstance As Integer) As String
    2.     Dim strOpenElement As String
    3.     Dim strCloseElement As String
    4.     Dim strTemp As String
    5.     Dim intIndex As Integer
    6.     Dim intStartPos As Integer
    7.     Dim intElementLength As Integer
    8.     strOpenElement = "<" & strElement & ">"
    9.     strCloseElement = "</" & strElement & ">"
    10.     intIndex = 0
    11.     If InStr(strDocument, strOpenElement) Then
    12.         While InStr(strDocument, strOpenElement) > 0
    13.             intIndex = intIndex + 1
    14.             intStartPos = InStr(strDocument, strOpenElement) + Len(strOpenElement)
    15.             intElementLength = InStr(strDocument, strCloseElement) - intStartPos
    16.             strTemp = Mid$(strDocument, intStartPos, intElementLength)
    17.             strDocument = Right$(strDocument, Len(strDocument) - (intStartPos + intElementLength))
    18.             If intIndex = intInstance Then
    19.                 ReturnSpecificElementInstance = strTemp
    20.             End If
    21.         Wend
    22.     End If
    23. 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
    Attached Files Attached Files
    Last edited by sciguyryan; May 23rd, 2005 at 05:55 PM.
    My Blog.

    Ryan Jones.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width