Results 1 to 8 of 8

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

  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.

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    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.

    One question: Why are you using the + sign rather than the & sign for concatenation?

  3. #3

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

    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.

    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
    Last edited by sciguyryan; May 23rd, 2005 at 01:55 PM.
    My Blog.

    Ryan Jones.

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    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.

  5. #5

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

    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
    My Blog.

    Ryan Jones.

  6. #6
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    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.

  7. #7

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

    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
    My Blog.

    Ryan Jones.

  8. #8

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

    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
    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