网络服务客户端开发:XML 与 JSON 解析及 RESTful 服务特性
1. XML 解析
在处理网络服务的响应时,XML 解析是一项常见的任务。不同的编程语言提供了多种 XML 解析方式。
1.1 Ruby 示例
以下是一个 Ruby 代码示例,用于从 del.icio.us 获取最近的书签信息:
xml = open('https://api.del.icio.us/v1/posts/recent',
:http_basic_authentication => [username, password])
# Feed the XML entity-body into a pull parser
parser = REXML::Parsers::PullParser.new(xml)
# Until there are no more events to pull...
while parser.has_next?
# ...pull the next event.
tag = parser.pull
# If it's a 'post' tag...
if tag.start_element?
if tag[0] == 'post'
# Print information about the bookmark.
attrs = tag[1]
puts "#{attrs['description']}: #{attrs['href']}"
end
end
end
end
#
超级会员免费看
订阅专栏 解锁全文
1485

被折叠的 条评论
为什么被折叠?



