首页
|
新闻
|
新品
|
文库
|
方案
|
视频
|
下载
|
商城
|
开发板
|
数据中心
|
座谈新版
|
培训
|
工具
|
博客
|
论坛
|
百科
|
GEC
|
活动
|
主题月
|
电子展
注册
登录
论坛
博客
搜索
帮助
导航
默认风格
uchome
discuz6
GreenM
»
MCU 单片机技术
»
PowerPC
» Atom/RSS feed 的应用 -5 为第三方服务提供 feed 数据视图
返回列表
回复
发帖
发新话题
发布投票
发布悬赏
发布辩论
发布活动
发布视频
发布商品
Atom/RSS feed 的应用 -5 为第三方服务提供 feed 数据视图
发短消息
加为好友
look_w
当前离线
UID
1066743
帖子
8283
精华
0
积分
4142
阅读权限
90
在线时间
233 小时
注册时间
2017-6-23
最后登录
2019-5-18
论坛元老
UID
1066743
1
#
打印
字体大小:
t
T
look_w
发表于 2018-8-23 20:05
|
只看该作者
Atom/RSS feed 的应用 -5 为第三方服务提供 feed 数据视图
为第三方服务提供 feed 数据视图虽然 feed 已经成为 Web 2.0 流行风潮中数据服务最常见的发布格式,但现实 Web2.0 世界仍旧存在大量数据服务并没有使用 RSS 或 Atom 等格式,如 Amazon Web Services,Ebay Web Services 等提供的 HTTP API。使用 WebSphere sMash 可以连接这类数据服务,并且提供将 XML 格式的数据转换成 feed 格式。
这里以 Last.fm 提供的 Track Search 服务为例,通过 XSL 和一个 groovy 方法来为 Last.fm 提供 feed 格式的数据视图。
1. 第一步当然是创建 sMash 应用程序,这里直接介绍接下来的步骤。打开 AppBuilder 的 Dependencies 标签中的 “Add” 按钮,选中 zero.assemble.flow。
图 10. 使用 zero.assemble.flow 模块
2. 为 TrackSearch 服务提供服务描述文件 app/assemble/services/LastfmSearch.xml。
清单 10. 服务描述文件片段
1
2
3
4
5
6
7
8
9
10
11
12
<?xml version="1.0" encoding="utf-8"?>
<entry xmlns="http://www.w3.org/2005/Atom"
xmlns:z="http://www.projectzero.org/assemble/component">
<title>Last.fm Web Services</title>
<link rel=
"http://www.projectzero.org/assemble/component/url-template"
type="text/xml"
href="http://ws.audioscrobbler.com/2.0/?method=
{method}&api_key={apiKey}&artist=
{artist?}&track={track}"
/>
......
3. 创建 XSL 文件 public/stylesheet/lastfm.xsl 将 Last.fm 提供的结果转换成为 Atom feed
清单 11. 格式转换文件片段
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/2005/Atom"
xmlns
pensearch="http://a9.com/-/spec/opensearch/1.1/">
<xsl:template match="/">
<feed><title>
Search result for <xsl:value-of select=
"lfm/results/opensearch
uery/@searchTerms"/>
</title>
<opensearch:totalResults>
<xsl:value-of select="lfm/results/opensearch:totalResults"/>
</opensearch:totalResults>
<xsl:apply-templates select="lfm/results/trackmatches/track"/>
</feed>
</xsl:template>
......
4. 在 config/zero.config 中填入你的 API Key:
/config/lastfm/apiKey = "Last.fm API Key:
http://www.last.fm/api/account
"
5. 最后创建 Groovy 脚本 public/testTrackSearch/index.groovy 调用这个 TrackSearch 服务。这里使用关键词 "Believe" 进行搜索:
清单 12. Groovy 脚本调用 Track Search 服务
1
2
3
4
5
6
7
8
def criteria = [method: "track.search", track: "Believe", format: "atom"]
// use service description file /app/assemble/services/LastfmSearch.xml
def results = invokeService("TrackSearch", criteria)
// rendering
request.view = "XML"
request.xml.output = results
headers.out."Content-Type" = "application/atom+xml"
render()
启动这个应用程序,在浏览器中输入 http://localhost:8080/testTrackSearch 即可看到如图 11 所示的 feed。
图 11. 为 Track Search 服务提供 feed
收藏
分享
评分
回复
引用
订阅
TOP
返回列表
电商论坛
Pine A64
资料下载
方案分享
FAQ
行业应用
消费电子
便携式设备
医疗电子
汽车电子
工业控制
热门技术
智能可穿戴
3D打印
智能家居
综合设计
示波器技术
存储器
电子制造
计算机和外设
软件开发
分立器件
传感器技术
无源元件
资料共享
PCB综合技术
综合技术交流
EDA
MCU 单片机技术
ST MCU
Freescale MCU
NXP MCU
新唐 MCU
MIPS
X86
ARM
PowerPC
DSP技术
嵌入式技术
FPGA/CPLD可编程逻辑
模拟电路
数字电路
富士通半导体FRAM 铁电存储器“免费样片”使用心得
电源与功率管理
LED技术
测试测量
通信技术
3G
无线技术
微波在线
综合交流区
职场驿站
活动专区
在线座谈交流区
紧缺人才培训课程交流区
意见和建议