Yahoo!検索Webサービス ウェブ検索のXmlDocumentクラスでXMLデータを解析するサンプルコード(VB.NET) †
Yahoo!検索Webサービスのウェブ検索機能を使って検索を行うサンプルです。こちらのサンプルと違い、xsd.exeを使わずに、XmlDocumentクラスでXMLデータを解析しています。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
| | <%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Protected Sub Button1_Click( _
ByVal sender As Object, ByVal e As System.EventArgs)
Dim requestUrl As String = _
"http://api.search.yahoo.co.jp/WebSearchService/V1/webSearch"
requestUrl += "?appid=XXXXX"
requestUrl += "&query=" + Server.UrlEncode(TextBox1.Text)
Dim request As System.Net.HttpWebRequest = _
CType(System.Net.WebRequest.Create(requestUrl), _
System.Net.HttpWebRequest)
Dim response As System.Net.HttpWebResponse = Nothing
Dim doc As New System.Xml.XmlDocument()
Try
response = CType(request.GetResponse(), _
System.Net.HttpWebResponse)
Dim strm As System.IO.Stream = response.GetResponseStream()
doc.Load(strm)
Catch ex As Exception
Label1.Text = "エラー:" + Server.HtmlEncode(ex.Message)
Return
Finally
If Not (response Is Nothing) Then
response.Close()
End If
End Try
Dim xmlNSM As New System.Xml.XmlNamespaceManager(doc.NameTable)
xmlNSM.AddNamespace("yjs", "urn:yahoo:jp:srch")
Dim resultSet As System.Xml.XmlNode = _
doc.SelectSingleNode("//yjs:ResultSet", xmlNSM)
Label1.Text = ""
If resultSet.Attributes("totalResultsReturned").Value = "0" Then
Label1.Text += "該当するページが見つかりませんでした。"
Else
Label1.Text += "<b>" + Server.HtmlEncode(TextBox1.Text) + _
"</b> の検索結果 約 " + _
resultSet.Attributes("totalResultsAvailable").Value + _
"件中 " + _
resultSet.Attributes("firstResultPosition").Value + _
" - " + _
resultSet.Attributes("totalResultsReturned").Value + _
" 件目<br />" + vbLf
Label1.Text += "<ul>" + vbLf
Dim node As System.Xml.XmlNode
For Each node In resultSet.SelectNodes("yjs:Result", xmlNSM)
Label1.Text += "<li><a href=""" + _
node.SelectSingleNode("yjs:ClickUrl", xmlNSM).InnerText + _
""">" + _
node.SelectSingleNode("yjs:Title", xmlNSM).InnerText + _
"</a>"
Next node
Label1.Text += "</ul>" + vbLf
End If
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Yahoo!ウェブ検索Webサービスのサンプル</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server"
OnClick="Button1_Click" Text="検索" /><br />
<br />
<asp:Label ID="Label1" runat="server"
EnableViewState="False"></asp:Label>
</div>
</form>
<!-- Begin Yahoo! JAPAN Web Services Attribution Snippet -->
<span style="margin:15px 15px 15px 15px">
<a href="http://developer.yahoo.co.jp/about">Webサービス by Yahoo! JAPAN</a>
</span>
<!-- End Yahoo! JAPAN Web Services Attribution Snippet -->
</body>
</html>
|