2011年6月28日火曜日

→→→webページ①

・XmlDocument
   XmlDocumentクラスはXML文書全体を表すクラス
      ファイルを読み込んで、それを元にしてXMLDocumentクラスの
      オブジェクトを生成するとXML文書の構造が解析される
    Dim doc As New XMLDocument()
      doc.LoadXML(source)
・XMLNode
   XMLNodeクラスはXML文書内の個々の要素を表すクラス
      XML文書内の最初の子要素を得て、変数xNodeに
      割り当てた後、その子要素の名前と値を表示するには
      Dim xNode As XMLNode
      xNode = doc.FirstChild
      Msgbox("名前=" & xNode.Name & "値=" & xNode.Value)


------------------------------------------------------------
Imports SystemNet
Imports System.Text
Imports System.IO
Imports System.Xml

Public Class Form1
  Private Sub Form1_Load(・・・
    Dim source As String = ""
    Dim doc As New XmlDocument
    Dim xNode As XmlNode
    Dim dataStr As String
    Dim dbDataStr As Date
    Dim weather As String = ""
    Dim isGetOK As Boolean
    Dim con As New OleDbConnection
    Dim cmd As New OleDbCommand
    Dim rdr As OleDbDataReader

    Try
            GetWebPageSource("http://・・・", Source ,"Uft-8")
            doc.LoadXml( source )
            xNode = doc.item("lwws").item("forecastdate")
            dataStr = xNode.lnnerXml
            dbDatestr = Format( CDate ( dateStr ), "yyyy/MM/dd" )
            xNode = doc.Item("lwws").item("telop")
            weather = xNode.InnerXml
            isGetOk = True
     Catch ex As Exception
            isGetOk = False
     End Try
------------------------------------------------------------

XML例
1: <?Xml Version="1.0"?>
2: <person>
3:      <name>山田太郎</name>
4:      <birthday format="jp">1980/3/15</age>
5:</person>


person要素

name要素          birth要素
値 = 山田太郎       値 = 1985/3/15

XmlNode.InnerXml ・・・ このノードの子ノードだけを表すマークアップを取得または設定
          Dim instance As XmlNode
          Dim value As String
          value = instance.InnerXml
          instance.InnerXml = value


------------------------------------------------------------
 Try
      with con
        .ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & _
                                    My.Application.Info.DirectoryPath & "\test.accdb"
        .Open()
     End with
     with cmd
        .CommandText = "SELECT COUNT(*) FROM weather WHERE 日付=" & _
                                dbDateStr & "'"
        .Connection = con
     End with
     If cmd.ExecuteScalar() = 0 And isGetOk Then
      with cmd
         .CommandText = "INSERT INTO weather value(" &_
                                 dbDataStr  & "','" & weather & "')"
         .Connection = con
      End with
      cmd.ExecuteNonQuert() 
     End If
     with cmd
         .CommandText = "SELECT * FROM weather ORDER BY 日付 DESC"
         .Connection = con
     End with
     rdr = cmd.ExecuteReader()


     Do while rdr.Read()
        Dim rowData As String = {rdr.GetString(0), rdr.GetString(1)}
        DataGridView1.Rows.Add(rowData)
     LOOP
     rdr.Close()
     con.Close()
 Catch ex As Exception
     MsgBox(ex.Message)
 End Try
End Sub
------------------------------------------------------------
WebRequestはインターネットからのデータにアクセスするための.NET Frameworkの要求/応答
モデル用のabstract基本クラス

Webページのソースを読み出すFunctionプロシージャ
------------------------------------------------------
Private Sub btnClick(・・・
 Dim TARGET As String = "http://dl.google.com/picasoweb-current-setup.exe"
  Dim DOWNLOADFILENAME As String = "D:\picasawbe-current-setup.exe"
  Dim request As HttpWebRequest
  Dim response As HttpWebResponse
  Dim stream As stream
  Dim fs As FileStream
  Dim buffer As Byte()
  Dim count As Integer


  request = CType( webRequest.Create(TARGET) , HttpWebRequest )
  response = CType( request.GetResponse() , HttpWebRespose )
  stream = response.GetResponseStream()
  fs = New FileStream( DOWNLOADFILENAME , FileMode.CreateNew )
  buffer = New Byte(1024){}
  Do
    count = Stream.Read( buffer , 0 , buffer.Length )
    fs.Write( buffer , 0, count )
  Loop while count <> 0


  Stream.Close()
  fs.Close()
  response.Close()
End Sub
------------------------------------------------------


-----------------------------------------------------------------
Function GetWebPageSource (ByVal URL As String, ByRef source As String, ByVal charCode As String) As Boolean

    Dim request As WebRequest
    Dim response As HttpWebResponse
    Dim dataStream As Stream
    Dim reader As StreamReader
    Try
           request = WebRequest.Create(URL)     '指定されたURLへのリクエストを生成する
           response = CType( request.GetResponse() , HttpWebResponse )  'レスポンスを得る
dataStream = Response.GetResponseStream()       'データストリームを得る
           reader = New StreamReader( dataStream, Encoding.GetEncoding( charCode ))
                                   'webページのソースを読み出すためのストリームリーダーを生成する
 source = reader.ReadToEnd          'webページのソースを読み出す
           reader.Close()
           dataStream.Close()
           response.Close()
           GetWebPageSource = True
      Catch ex As Exception
           GetWebPafeSource = False
      End Try
End Function









     

0 件のコメント:

コメントを投稿