반응형

* VBNET 기상청 날씨 (Weather) 정보 가져오기 예제...

 

메인화면

전체 소스 코드

Form1.vb

========================================================================

Public Class Form1

    Dim strURL As String = "http://www.kma.go.kr/weather/forecast/mid-term-xml.jsp"
    Dim strCity As String = ""

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        cboCity.SelectedIndex = 10
        lblToday.Text = DateTime.Now.ToString("yyyy-MM-dd")
    End Sub

    Private Sub cboCity_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboCity.SelectedIndexChanged
        Try

            Using xr As Xml.XmlReader = Xml.XmlReader.Create(strURL)

                Dim strMsg As String = ""
                Dim ws As Xml.XmlWriterSettings = New Xml.XmlWriterSettings()
                ws.Indent = True
                Dim bCheck As Boolean = False
                Dim iCount As Integer = 0

                strCity = cboCity.Text

                While (xr.Read())

                    Select Case xr.NodeType
                        Case Xml.XmlNodeType.CDATA
                            '헤더 내용 표시
                            txtMsg.Text = xr.Value.ToString().Replace(", ")
                        Case Xml.XmlNodeType.Element
                        Case Xml.XmlNodeType.Text
                            '선택된 도시이면...
                            If (xr.Value.Equals(strCity)) Then
                                bCheck = True
                            End If

                            If (bCheck) Then
                                Dim dt As DateTime
                                Dim b As Boolean = DateTime.TryParse(xr.Value.ToString(), dt)

                                If b Then
                                    strMsg += "/"
                                End If

                                strMsg += xr.Value + ","
                                iCount += 1
                                If (iCount > 36) Then
                                    bCheck = False
                                End If

                            End If

                        Case Xml.XmlNodeType.XmlDeclaration
                        Case Xml.XmlNodeType.ProcessingInstruction
                        Case Xml.XmlNodeType.Comment
                        Case Xml.XmlNodeType.EndElement
                    End Select

                End While

                '요일별로 짜르기
                Dim strTmp() As String = strMsg.Split("/")

                '요일별 데이터로 나누기
                Dim strWh1() As String = strTmp(1).Split(",")
                label3.Text = strWh1(0)
                label5.Text = "최저: " + strWh1(2) + " ℃"
                label6.Text = "최고: " + strWh1(3) + " ℃"
                label7.Text = strWh1(1)

                Dim strWh2() As String = strTmp(2).Split(",")
                label11.Text = strWh2(0)
                label10.Text = "최저: " + strWh2(2) + " ℃"
                label9.Text = "최고: " + strWh2(3) + " ℃"
                label8.Text = strWh2(1)

                Dim strWh3() As String = strTmp(3).Split(",")
                label15.Text = strWh3(0)
                label14.Text = "최저: " + strWh3(2) + " ℃"
                label13.Text = "최고: " + strWh3(3) + " ℃"
                label12.Text = strWh3(1)

                Dim strWh4() As String = strTmp(4).Split(",")
                label27.Text = strWh4(0)
                label26.Text = "최저: " + strWh4(2) + " ℃"
                label25.Text = "최고: " + strWh4(3) + " ℃"
                label24.Text = strWh4(1)

                Dim strWh5() As String = strTmp(5).Split(",")
                label23.Text = strWh5(0)
                label22.Text = "최저: " + strWh5(2) + " ℃"
                label21.Text = "최고: " + strWh5(3) + " ℃"
                label20.Text = strWh5(1)

                Dim strWh6() As String = strTmp(6).Split(",")
                label19.Text = strWh6(0)
                label18.Text = "최저: " + strWh6(2) + " ℃"
                label17.Text = "최고: " + strWh6(3) + " ℃"
                label16.Text = strWh6(1)

            End Using

        Catch ex As Exception

        End Try
    End Sub

    
End Class

========================================================================

기상청 jsp 내용

 

콤보 박스 내용은 아래의 그림과 같이 속성에서 바로 등록 해 주었으며, ComboBox Index 가 아닌

Text 즉 도시 이름으로 체크 하기에 아이템 순서는 상관이 없습니다.

 

 

*결과 화면

 

 

반응형

+ Recent posts