get新技能是需要付出行动的,即使看得再多也还是要动手试一试。今天小编跟大家分享的是在 Atlas中调用Web Service—创建Mashup调用远端Web Serv,一起来学习了解下吧!
作者:Dflying Chen
在前一篇文章(在中调用 Service——创建Mashup调用远端Web Service(基础知识以及简单示例))中,我介绍了一些Atlas中对远程Web Service进行Mashup的基础知识,并给出了一个最基础的没有丝毫用处例子。今天再回到这个话题上,我将给出一个更复杂点的,但有一些用处的例子——Yahoo! Weather。
废话到此为止,让我们先熟悉一下Yahoo! Weather服务:Yahoo!在其网站上提供了天气预报服务,并且它也提供了 Service的接口
从上面两个网页上面,我们可以知道Yahoo!提供的天气Service的URL为,该服务还有两个参数:
p:要查询天气的地点代码(可以在查询到不同地方的这个代码)。
u:返回结果中温度的单位,f代表华氏度,c代表摄氏度。
看来这个Yahoo! Weather服务还挺简单的,让我们测试下好不好用。先到查出来上海的地点代码为。然后在浏览器中输入?&,嗯,返回了如下的一段不是很复杂的:
Yahoo Weather Service XML Result
?xml version="1.0" encoding="UTF-8" standalone="yes" ?
rss version="2.0" "" "#"
channel
titleYahoo! Weather - Shanghai, CH/title
*
descriptionYahoo! Weather for Shanghai, CH/description
languageen-us/language
lastBuildDateThu, 25 May 11:00 amCST/lastBuildDate
ttl60/ttl
city="Shanghai" region="" country="CH" /
temperature="C" distance="km" pressure="mb" speed="kph" /
chill="21" direction="260" speed="14" /
humidity="78" visibility="299" pressure="0" rising="0" /
sunrise="4:52 am" sunset="6:50 pm" /
image
titleYahoo! Weather/title
width142/width
height18/height
/image
item
titleConditions for Shanghai, CH at 11:00 am CST/title
*
pubDateThu, 25 May 11:00 am CST/pubDate
text="Fog" code="20" temp="21" date="Thu, 25 May 11:00 am CST" /
description
![CDATA[
img src="http://us.//i/us/we/52/20.gif" /br /
bCurrent /
Fog, 21 CBR /BR /
/
Thu - Scattered Thunderstorms. High: 25 Low: 20br /
Fri - AM Showers. High: 26 Low: 18br /
br /
a href="Full'>http://us./dailynews/rss/weather/Shanghai__CH/*http://xml./forecast/CHXX0116_c.html"Full Forecast at Yahoo! Weather/aBR/
(provided by The Weather Channel)br/
]]
/description
day="Thu" date="25 May " low="20" high="25" text="Scattered Thunderstorms" code="38" /
day="Fri" date="26 May " low="18" high="26" text="AM Showers" code="39" /
guid isPermaLink="false"CHXX0116__05_25_11_0_CST/guid
/item
/channel
/rss
!-- uncompressed/chunked Thu May 25 20:49:07 PDT --
我们可以看到,它提供的信息非常全面(连日出日落时间都有……),下面让我们书写asbx Bridge页面来对这个Service进行Mashup。
首先,参考在 Atlas中调用Web Service——创建Mashup调用远端Web Service(基础知识以及简单示例)这篇文章中的那个asbx的声明,我们可以写出如下一段:
?xml version="1.0" encoding="utf-8" ?
bridge namespace="Dflying" className="YahooWeatherService"
proxy type=""
serviceUrl="" /
method name="GetWeather"
input
parameter name="p" /
parameter name="u" value="c" /
/input
/method
/bridge
其中:
bridge的namespace和className属性以及method的name属性让我们在客户端JavaScript中可以通过()这样的方法签名来访问这个。
proxy的serviceUrl属性指定了Yahoo! Weather Service的URL。
GetWeather方法中定义了上面列出来的p和u两个参数,其中u参数我们指定了它的默认值为c(代表摄氏度),p参数将由调用者负责传过来。
写到这一步其实也够了,客户端将收到上面浏览器中看到的那一段XML String,并且可以在客户端进行处理并显示。但客户端对XML的处理并不是那么容易,也不是那么高效,同时通过网络传输太多不必要的信息也是一种浪费。所以这里我们利用asbx中内建的Transformer对这段XML处理一下,提取出我们感兴趣的内容并以JSON的形式发给客户端。在method段中加入下面一段:
transforms
transform type=""
data
attribute name="selector" value="channel" /
dictionary name="namespaceMapping"
item name="yweather" value="" /
/dictionary
dictionary name="selectedNodes"
item name="Title" value="title" /
item name="Description" value="item/description" /
item name="CurrentCondition" value="item/yweather:[email protected]" /
/dictionary
/data
/transform
/transforms
其中transforms声明表示这个Mashup方法的返回值将会被一些transformer改变一下,里面声明了一个类型为的transformer,表示将用XPath表达式来转换。在这个XPathBridgeTransformer中要声明如下部分:
name为selector的一个attribute段,其中指定的value属性为一个XPath表达式,将选取整个XPathBridgeTransformer将用到的数据段。
name为namespaceMapping的一个dictionary段,其中指定了这个XML文件中的namespace映射。如果在下面的选择节点过程中我们用到了某个namespace,那么这里就必须有它的声明。这里我们在其中添加一个对yweather的映射,因为下面要用到。
name为selectedNodes的一个dictionary段,其中每一个item的value属性是一个XPath String,用来从XML中选择出相应的值,name属性用来指定相应的在JavaScript中的属性名称。这里作为示例,我只取得其中三段内容,您可以看到,其中CurrentCondition的XPath中用到了上面指定的namespaceMapping。
关于XPath的知识,我就不多讲了,感兴趣或是不太熟悉的朋友可以自行Google,网上资源很多。关于其他类型的Transformer,我也不是很熟悉,今后如果遇到了我再讲讲。完成后的文件如下:
?xml version="1.0" encoding="utf-8" ?
bridge namespace="Dflying" className="YahooWeatherService"
proxy type=""
serviceUrl="" /
method name="GetWeather"
input
parameter name="p" /
parameter name="u" value="c" /
/input
transforms
transform type=""
data
attribute name="selector" value="channel" /
dictionary name="namespaceMapping"
item name="yweather" value="" /
/dictionary
dictionary name="selectedNodes"
item name="Title" value="title" /
item name="Description" value="item/description" /
item name="CurrentCondition" value="item/yweather:[email protected]" /
/dictionary
/data
/transform
/transforms
/method
/bridge
现在创建一个 Page测试一下,首先依然是重复了一千遍的ScriptManager,还有对Bridge的引用: runat="server"
Services
Path="" /
/Services
/
然后一个HTML Select元素,里面列入了几个城市以及相应的城市代码:
!-- place selector --
select
option selected="selected" value="CHXX0116"Shanghai, CH/option
option value="USCA0746"Mountain View, CA/option
option value="CHXX0008"Beijing, CH/option
/select
一个HTML Button,用来触发对Service的调用:
!-- invoke the service --
input type="button" value="Get Weather" /
一段HTML用来显示结果:
!-- display result --
div
divTitle/div
div/div
divDescription/div
div/div
/div
然后是JavaScript,可以看到通过()调用了,并在方法返回后把经过的值输出到了页面上: function getWeather_onclick() {
// new atlas 'Select' control
var place = new ($());
// invoke the bridge method
({'p': ()}, onGetComplete);
}
function onGetComplete(result) {
$('result'). = "block";
$('title').innerHTML = result[0].Title;
$('description').innerHTML = result[0].Description;
}