拆解 XML 取得 RSS 股市新聞標題
本文的重點在於幫助交易員拆解 XML 取得 RSS 股市新聞標題, 取得新聞標題最大的用意在於預警, 可避免投資標的物暴漲或暴跌, 甚至搶得投資先機 !
簡單的說, RSS 顯示出的網頁會是以 XML 為基礎的文字檔案, 自行拆解比使用一般市面上的 RSS Reader 閱讀器最大優點在於自己可以掌握主動通知, 可以容易的結合自行開發的程式, 亦可以定義自己需要的【關鍵字】提醒 !
首先, 我們先用 WebBrowser1.DocumentType.ToString() = "XML Document" 來驗證是否為 XML 格式, 再使用 Net.WebClient 的 DownloadData 將 XML 全部抓回來處理, 最後, 並將每個節點資訊拆開即可 !
Private Sub WebBrowser1_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
Try
If WebBrowser1.DocumentType.ToString() = "XML Document" Then
'下載 XML
Dim myWebClient As New Net.WebClient
Dim myByte() As Byte
Dim strXML As String = ""
myByte = myWebClient.DownloadData(TextBox1.Text.ToString())
Select Case TextBox2.Text.Trim.ToUpper()
Case "UTF-8".Trim.ToUpper()
strXML = Encoding.UTF8.GetString(myByte, 0, myByte.Length)
Case "UTF-16".Trim.ToUpper()
strXML = Encoding.Unicode.GetString(myByte, 0, myByte.Length)
Case "UTF-32".Trim.ToUpper()
strXML = Encoding.UTF32.GetString(myByte, 0, myByte.Length)
Case "ASCII".Trim.ToUpper()
strXML = Encoding.ASCII.GetString(myByte, 0, myByte.Length)
Case Else
If TextBox2.Text.Trim.Length > 0 Then
Try
myByte = Encoding.Convert(Encoding.Default, Encoding.GetEncoding(TextBox2.Text.Trim), myByte)
strXML = Encoding.Default.GetString(myByte, 0, myByte.Length)
Catch exErr As Exception
strXML = ""
End Try
Else
strXML = ""
End If
End Select
'拆解 XML
Dim strNode() As String
strNode = Split(strXML, "")
'讀取節點
If strNode.Length > 1 Then
Dim iIndex As Integer = 0
For iIndex = strNode.Length - 2 To 0 Step -1
If iIndex = 0 Then
If InStr(strNode(0), "
Dim strTmp() As String
strTmp = Split(strNode(0), "
Else
strNode(0) = ""
End If
End If
Dim strAddTitle As String = ""
Dim strAddUrl As String = ""
Dim strAddTime As String = ""
'抓取
Try
Dim iFrom As Integer = 0
Dim iTo As Integer = 0
Dim strCode As String = ""
strCode = "title"
iFrom = InStr(strNode(iIndex), "<" + strCode + ">")
iTo = InStr(strNode(iIndex), "" + strCode + ">")
If iFrom > 0 And iTo > 0 Then
strAddTitle = Mid(strNode(iIndex), iFrom + strCode.Length + 2, iTo - CInt(iFrom + strCode.Length + 2))
strAddTitle = strAddTitle.Replace("", "").Trim()
End If
Catch exCatch As Exception
End Try
'抓取
Try
Dim iFrom As Integer = 0
Dim iTo As Integer = 0
Dim strCode As String = ""
strCode = "link"
iFrom = InStr(strNode(iIndex), "<" + strCode + ">")
iTo = InStr(strNode(iIndex), "" + strCode + ">")
If iFrom > 0 And iTo > 0 Then
strAddUrl = Mid(strNode(iIndex), iFrom + strCode.Length + 2, iTo - CInt(iFrom + strCode.Length + 2))
strAddUrl = strAddUrl.Replace("", "").Trim()
End If
Catch exCatch As Exception
End Try
'抓取
Try
Dim iFrom As Integer = 0
Dim iTo As Integer = 0
Dim strCode As String = ""
strCode = "pubDate"
iFrom = InStr(strNode(iIndex), "<" + strCode + ">")
iTo = InStr(strNode(iIndex), "" + strCode + ">")
If iFrom > 0 And iTo > 0 Then
strAddTime = Mid(strNode(iIndex), iFrom + strCode.Length + 2, iTo - CInt(iFrom + strCode.Length + 2))
strAddTime = strAddTime.Replace("", "").Trim()
End If
Catch exCatch As Exception
End Try
RichTextBox1.Text = RichTextBox1.Text + "-----------------------------------------------------------------------------------------------" + vbNewLine
RichTextBox1.Text = RichTextBox1.Text + "Title: " + strAddTitle + vbNewLine
RichTextBox1.Text = RichTextBox1.Text + "Url: " + strAddUrl + vbNewLine
RichTextBox1.Text = RichTextBox1.Text + "Time: " + strAddTime + vbNewLine
Next
End If
Else
MessageBox.Show("不是 XML Document", "有誤", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If
Catch ex As Exception
MessageBox.Show(ex.ToString, "Error Message - WebBrowser1_DocumentCompleted()", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
VB.NET 2005 範例下載 http://cat.hfu.edu.tw/~m9025004/download/GetNewsTitle.zip