Results 1 to 25 of 25

Thread: [RESOLVED] Getting weather in VB

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2007
    Location
    Canada
    Posts
    297

    Resolved [RESOLVED] Getting weather in VB

    Does anyone know how to impliment or have / know of a code that can download weather data? Maybe apply cerain sections to certain labels?

  2. #2
    Member
    Join Date
    Nov 2007
    Posts
    39

    Re: Getting weather in VB

    Where are you wanting to download this data from?

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2007
    Location
    Canada
    Posts
    297

    Re: Getting weather in VB

    I was hoping from Environment Canada...

    Here is a sample XML feed:
    http://www.weatheroffice.gc.ca/rss/city/on-118_e.xml

    I just want to be basically able to send each XML tag to a label.. Current Conditions to one label, maybe if possible seperate the current conditions elements by the <BR> tag (If you look at the feed, all of the conditions are in one caption, but each condition is seperated by a <br> tag.) Send the forecast tags to another label, etc, etc..

    Possible? Thanks for responding!

  4. #4
    Member
    Join Date
    Nov 2007
    Posts
    39

    Re: Getting weather in VB

    Doogle had written this code for my program to parse data from a web site.

    Maybe you could modify it for your needs.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2007
    Location
    Canada
    Posts
    297

    Re: Getting weather in VB

    I think I've figured it out.. I can just have it download the file, open it, and display it by array to a label?

    Once I do that, it would copy the whole array, like this:
    Code:
      <title>Ottawa (Kanata - Orl&#233;ans) - Weather - Environment Canada</title>
    So how could I tell it to remove the "<title>" and "</title>" tag before it sends the array to a label?

  6. #6
    Member
    Join Date
    Nov 2007
    Posts
    39

    Re: Getting weather in VB

    This code can help you download the page:

    Code:
    Private Sub cmdUpdate_Click()
        '   Dim objLink As HTMLLinkElement
        Dim objMSHTML       As New MSHTML.HTMLDocument
        Dim objDocument     As MSHTML.HTMLDocument
        Dim lngResult       As Long
        Dim strSource       As String
        Dim intFileNum      As Integer
        Dim strURL          As String
        Dim intCounter      As Integer
        Dim LastRow         As Integer
        
        cmdUpdate.Enabled = False
        
        GatherDate = Format(Date, "mm-dd-yyyy")
        lblStatus.Caption = "Gettting document via HTTP"
        
        strURL = "ENTER YOUR URL HERE"
        ' This function is only available with Internet Explorer 5
        Set objDocument = objMSHTML.createDocumentFromUrl(strURL, vbNullString)
        
        lblStatus.Caption = "Getting and parsing HTML document"
        
        ' Tricky, to make the function wait for the document to complete, usually
        ' the transfer is asynchronus. Note that this string might be different if
        ' you have another language than english for Internet Explorer on the
        ' machine where the code is executed.
        
        While objDocument.readyState <> "complete"
            DoEvents
        Wend
        
        lblStatus.Caption = "Document completed"
        
        ' Copying the source to the text box
        
        strSource = objDocument.documentElement.outerHTML
        
        DoEvents
        
        ' Copying the title of the page to the label
        
        lblTitle.Caption = "Title : " & objDocument.Title
        
        DoEvents
        
        Beep
        
        lblStatus.Caption = "Storing Text"
        
        intFileNum = FreeFile()
                    
        strFile = Format(Date, "mm-dd-yyyy")
        strFile = App.Path + "\Log\" + strFile + ".txt"       ' Create user file name
    
        Open strFile For Output As #intFileNum
        
            Write #intFileNum, strSource
        
        Close #intFileNum
        
        lblStatus.Caption = "Configuring Data"
    
        Call ConfigData
        
        lblStatus.Caption = "Storing Data"
        
        Call StoreData
        
     End Sub
    Then this is what I did to configure the data.

    Code:
    Private Sub ConfigData()
    
    Dim strData As String
    Dim strLine As String
    Dim intFile As Integer
    Dim intI As Integer
    '
    ' Getting the data
    '
    intFile = FreeFile
    Open strFile For Input As #intFile
    Do
        Line Input #intFile, strLine
        strData = strData & strLine & vbCrLf
    Loop Until EOF(intFile)
    Close #intFile
    
    Call ParseData(strData)
    
    End Sub
    You can then use the parsing in the link above (modifying it to your need) and storing the fields either to the labels or to an array for example.

    Hope this helps.
    Last edited by Powerman4160; Jan 7th, 2008 at 11:39 PM.

  7. #7
    Frenzied Member
    Join Date
    Dec 2007
    Posts
    1,072

    Re: Getting weather in VB

    Very Possible.

    Get the source of the xml feed via INET.

    Try this code:

    Code:
    Public Function iBetween(ByVal strVariable As String, _
                                str1 As String, str2 As String, _
                                    Optional lngBeginning As Long = 1) As String
    On Error Resume Next
    ' icey defeats iBetween
    Dim a As Long
    Dim b As Long
    Dim c As Long
    
    b = InStr(lngBeginning, strVariable, str1)
    c = InStr(b + Len(str1), strVariable, str2)
    
    If IsNull(b) Or IsNull(c) Then
        iBetween = vbNullString
        Exit Function
    End If
    
    strVariable = Replace(strVariable, Right(strVariable, Len(strVariable) - c + 1), vbNullString)
    
    strVariable = Right(strVariable, Len(strVariable) - InStr(lngBeginning, strVariable, str1, vbTextCompare) - Len(str1) + 1)
    
    iBetween = strVariable
    
    End Function
    
    Private Sub Command1_Click ()
    Dim strA As String
    Dim strB As String
    
              Do Until strA = vbNullString
          strA = iBetween (strofXMLFeed, "&#60;b>", "&#60;/b>")
          strB = iBetween (strofXMLFeed, "&#60;/b>", "&#60;/b>")
               strofXMLFeed = Replace (strofXMLFeed, "&#60;b>" & strA & "&#60;/b>", vbNullString)
               strofXMLFeed = Replace (strofXMLFeed, "&#60;b>" & strB & "&#60;/b>", vbNullString)
         List1.AddItem strA & " " & strB
              Loop
    End Sub
    Should get you started...

    Wrote off the top of my head. (There are probably about 20 replies now )

  8. #8
    Frenzied Member
    Join Date
    Dec 2007
    Posts
    1,072

    Re: Getting weather in VB

    Quote Originally Posted by Powerman4160
    This code can help you download the page:

    Code:
    Private Sub cmdUpdate_Click()
        '   Dim objLink As HTMLLinkElement
        Dim objMSHTML       As New MSHTML.HTMLDocument
        Dim objDocument     As MSHTML.HTMLDocument
        Dim lngResult       As Long
        Dim strSource       As String
        Dim intFileNum      As Integer
        Dim strURL          As String
        Dim intCounter      As Integer
        Dim LastRow         As Integer
        
        cmdUpdate.Enabled = False
        
        GatherDate = Format(Date, "mm-dd-yyyy")
        lblStatus.Caption = "Gettting document via HTTP"
        
        strURL = "ENTER YOUR URL HERE"
        ' This function is only available with Internet Explorer 5
        Set objDocument = objMSHTML.createDocumentFromUrl(strURL, vbNullString)
        
        lblStatus.Caption = "Getting and parsing HTML document"
        
        ' Tricky, to make the function wait for the document to complete, usually
        ' the transfer is asynchronus. Note that this string might be different if
        ' you have another language than english for Internet Explorer on the
        ' machine where the code is executed.
        
        While objDocument.readyState <> "complete"
            DoEvents
        Wend
        
        lblStatus.Caption = "Document completed"
        
        ' Copying the source to the text box
        
        strSource = objDocument.documentElement.outerHTML
        
        DoEvents
        
        ' Copying the title of the page to the label
        
        lblTitle.Caption = "Title : " & objDocument.Title
        
        DoEvents
        
        Beep
        
        lblStatus.Caption = "Storing Text"
        
        intFileNum = FreeFile()
                    
        strFile = Format(Date, "mm-dd-yyyy")
        strFile = App.Path + "\Log\" + strFile + ".txt"       ' Create user file name
    
        Open strFile For Output As #intFileNum
        
            Write #intFileNum, strSource
        
        Close #intFileNum
        
        lblStatus.Caption = "Configuring Data"
    
        Call ConfigData
        
        lblStatus.Caption = "Storing Data"
        
        Call StoreData
        
     End Sub
    Then this is what I did to configure the data.

    Code:
    Private Sub ConfigData()
    
    Dim strData As String
    Dim strLine As String
    Dim intFile As Integer
    Dim intI As Integer
    '
    ' Getting the data
    '
    intFile = FreeFile
    Open strFile For Input As #intFile
    Do
        Line Input #intFile, strLine
        strData = strData & strLine & vbCrLf
    Loop Until EOF(intFile)
    Close #intFile
    
    Call ParseData(strData)
    
    End Sub
    You can then use the parsing in the link above (modifying it to your need) and storing the fields either to the labels or to an array for example.

    Hope this helps.
    Is that for .NET?

    If you need a good way to parse data, I posted a HTTPWrapper control in the VB6 CodeBank, it is an attachment in the Download Checker Source.

  9. #9
    Member
    Join Date
    Nov 2007
    Posts
    39

    Re: Getting weather in VB

    Quote Originally Posted by Zach_VB6
    Is that for .NET?
    Nope, it is all VB6.

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2007
    Location
    Canada
    Posts
    297

    Re: Getting weather in VB

    Even if it is VB6, its not doing anything :/

    I'm not too experienced with VB Internet controls.

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

    Re: Getting weather in VB

    Are you getting an error or just nothing at all?

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2007
    Location
    Canada
    Posts
    297

    Re: Getting weather in VB

    Nothing at all..

  13. #13
    Frenzied Member
    Join Date
    Dec 2007
    Posts
    1,072

    Re: Getting weather in VB

    Quote Originally Posted by wxmancanada
    Nothing at all..
    LOL!

    ' This function is only available with Internet Explorer 5

  14. #14
    Frenzied Member
    Join Date
    Apr 2003
    Location
    The Future - Skynet
    Posts
    1,157

    Re: Getting weather in VB

    Quote Originally Posted by Powerman4160
    Code:
    Private Sub cmdUpdate_Click()
        '   Dim objLink As HTMLLinkElement
        Dim objMSHTML       As New MSHTML.HTMLDocument
        Dim objDocument     As MSHTML.HTMLDocument
    Don't you have to reference a library for this? wxmancanada, that might be why you are not getting anything.
    I'll Be Back!

    T-1000

    Microsoft .Net 2005
    Microsoft Visual Basic 6
    Prefer using API

  15. #15
    Member
    Join Date
    Nov 2007
    Posts
    39

    Re: Getting weather in VB

    The program that I took that out of was parsing html pages and not xml pages. When I ran it to pull the data from the weather site, I only got <html><Body></body></html>.

    I have been looking into changing it to parse xml, but my knowledge is limited.

  16. #16

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2007
    Location
    Canada
    Posts
    297

    Re: Getting weather in VB

    Well, I think thanks to the help of Doogle, I've gotten a working code:

    Code:
    Private Function RemoveTags(ByVal strData As String) As String
    '
    ' Remove XML Tags in strData
    '
    ' Method:
    '       Locate "<" and ">" and replace them and everything between them with vbCR
    '       Repeat for all occurrences
    '       When finshed, replace all vbCr with null
    '       Return
    '
    Dim lngI As Long
    Dim lngPos As Long
    Dim lngPos1 As Long
    Dim boFinsihed As Boolean
    Dim strOut As String
    lngPos = 1
    strData = Replace(strData, "&#60", "<")
    strData = Replace(strData, vbLf, vbCrLf)
    Do
        lngPos = InStr(lngPos, strData, "<")
        If lngPos > 0 Then
            lngPos1 = InStr(lngPos + 1, strData, ">")
            If lngPos1 > 0 Then
                For lngI = lngPos To lngPos1
                    Mid(strData, lngI, 1) = Chr(255)
                Next lngI
                lngPos = lngPos1 + 1
            Else
                Mid(strData, lngPos, 1) = Chr(255)
                lngPos = lngPos + 1
            End If
        Else
            RemoveTags = strData
            bofinished = True
        End If
    Loop Until bofinished = True
    RemoveTags = Replace(strData, Chr(255), "")
    End Function
    
    Private Sub Command1_Click()
    '
    ' Remove Tags and display in textBox (txtOut)
    ' txtOut should be a Multilined TextBox with Scroll Bars if required
    '
    Dim strArr() As String
    Dim strData As String
    Dim ff As Integer
        ff = FreeFile
        'On Error Resume Next
        Open "C:\Weather.txt" For Input As #ff
        If Err Then
            MsgBox Err.Description
            Err.Clear
            Close #ff
            Exit Sub ' or change to Exit Function if appropriate
        Else
            'On Error GoTo 0
            strArr() = Split(Input(LOF(ff), ff), vbCrLf)
            For lngI = LBound(strArr) To UBound(strArr)
                txtOut.Text = txtOut.Text & RemoveTags(strArr(lngI))
            Next lngI
     End If
    End Sub
    I'm just confused, once the tags are removed, how to I send each array (line) to a seprate text box?

    Like for example, send array 10 to Label1, or send array 11 to Label2, etc?

  17. #17
    Frenzied Member
    Join Date
    Apr 2003
    Location
    The Future - Skynet
    Posts
    1,157

    Re: Getting weather in VB

    Quote Originally Posted by Powerman4160
    The program that I took that out of was parsing html pages and not xml pages. When I ran it to pull the data from the weather site, I only got <html><Body></body></html>.

    I have been looking into changing it to parse xml, but my knowledge is limited.
    You can use the XML library to parse XML. I done it before and will look for the code later on tonight.

    Nevertheless, that program probably reference a library which I can't remember off the top of my head.
    I'll Be Back!

    T-1000

    Microsoft .Net 2005
    Microsoft Visual Basic 6
    Prefer using API

  18. #18
    Member
    Join Date
    Nov 2007
    Posts
    39

    Re: Getting weather in VB

    Quote Originally Posted by wxmancanada

    I'm just confused, once the tags are removed, how to I send each array (line) to a seprate text box?

    Like for example, send array 10 to Label1, or send array 11 to Label2, etc?
    So, you mean something like label1.caption = array(10) ?

  19. #19

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2007
    Location
    Canada
    Posts
    297

    Re: Getting weather in VB

    Quote Originally Posted by Powerman4160
    So, you mean something like label1.caption = array(10) ?
    Yep, thats what I'm looking for, but I want the array to be the text that has the tags removed, not the arrays in the original file.

  20. #20
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Getting weather in VB

    Simplest way would be to assign the un-tagged data to another array rather than appending the data to the TextBox.
    Code:
        Else
            'On Error GoTo 0
            strArr() = Split(Input(LOF(ff), ff), vbCrLf)
            For lngI = LBound(strArr) To UBound(strArr)
                txtOut.Text = txtOut.txt & RemoveTags(strArr(lngI))
            Next lngI
    would change to
    Code:
    Dim strResults() As String
        Else
            On Error GoTo 0
            strArr() = Split(Input(LOF(ff), ff), vbCrLf)
            For lngI = LBound(strArr) To UBound(strArr)
                ReDim Preserve strResults(lngI)
                strResults(lngI) = RemoveTags(strArr(lngI))
            Next lngI
            '
            ' strResults now holds the un-tagged data, 1 line per element
            ' so you can assign each element to a Label
            ' eg Lab1.Caption = strResults(0)
            '    Lab2.Caption = strResults(1)
            ' etc
    Or you could use a Dynamic Label Control Array - but let's see how you get on with this first

    EDIT: I should add that the resulting data is terminated by a vbCrLf pair so you'll see a couple of "blobs" at the end of the information in the Label's caption since Labels can't be multi-lined (in the sense that they don't understand vbCrLf). To avoid that, use something like
    Code:
    Lab1.Caption = Left$(strResults(0), Len(strResults(0))-2)
    But I, personally wouldn't use Labels.
    Last edited by Doogle; Jan 10th, 2008 at 01:49 AM.

  21. #21

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2007
    Location
    Canada
    Posts
    297

    Re: Getting weather in VB

    Thanks Doogle, Ill give it a shot when I get home.. Why wouldnt you use labels for this?

  22. #22
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Getting weather in VB

    I wouldn't use Labels because you're limited in the amount of formatting and control over the size etc you have. Looking at the data involved thare might be a lot or just a little. Whilst you can autozise a Label it just may autosize itself over another one or autosize a part of itself off the Form altogether. If you don't use autosize you may not be able to display all the information. (since you don't know how much you've got until you've got it)

    Using Multi-lined textboxes (or RichTextBoxes) with scroll bars will allow you to fix the size of the controls and not 'loose' ay information when it's displayed. The user will be able to scroll through the data.

  23. #23

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2007
    Location
    Canada
    Posts
    297

    Re: Getting weather in VB

    Doogle, I changed my code around, and got it to display the XML with out the tags, but its not seprating the arrays. Only array 0 and 1 work. Array 0 is ALL the data and array 1 is blank. Any ideas on why?

    The XML file is one big line when I view it in Notepad, but when I view it in a label, its all spaced out in lines properly..

    The error I get if I go above array 1 is to like array 2 for example:
    "Subscript out of range"
    Last edited by wxmancanada; Jan 11th, 2008 at 06:39 PM.

  24. #24

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2007
    Location
    Canada
    Posts
    297

    Re: Getting weather in VB

    Scratch that, I figured out its just the way the file gets transfered or downloaded in to the comp..

    How do I go about telling it to remove a certain set of words or letters?

    Lets say I want to remove the text "#xb0;" from the document, or remove the word "Temp" how do I incorporate that in to the tag removal code?

  25. #25
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Getting weather in VB

    You can use the Replace Function. If you look at Function RemoveTags, at the end I use Replace to change every occurrence of Chr(255) with Null, you could put additional Replaces in front of that one
    eg
    Code:
    strData = Replace(strData, "#xb0;", "")

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