Web服务客户端开发:XML解析、JSON处理与REST服务特性
1. XML解析器在不同编程语言中的应用
在处理Web服务响应时,XML解析是常见的需求。不同编程语言提供了各种XML解析器,以下是一些常见语言的相关解析器介绍。
1.1 Ruby
以下是一个Ruby代码示例,用于解析XML并打印书签信息:
# 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
# Main program.
username, password = ARGV
unless username and password
puts "Usage: #{$0} [USERNAME] [PASSWORD]"
e
超级会员免费看
订阅专栏 解锁全文
1270

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



