:( ruby以webserver的形式读取xml文件。
------------格式为-------------------------
:( ruby以webserver的形式读取xml文件。
------------格式为-------------------------
如果是简单的、标准的可以用Net::HTTP和rexml
如果复杂点不规则的,比如解析网页 可以用'hpricot'
HTTP + Rexml:
[code="ruby"]
require 'net/http'
require 'rexml/document'
Net::HTTP.start('www.abc.com',80) do |http|
# 注意 start('www.abc.com',80) 这里的 www.abc.com 不带 http://
response = http.get("/company/abc.html")
# http.get("/company/abc.html") 是你要获取文件的路径
result_http_state = response.message
if result_http_state == "OK"
result_info = response.body
doc = REXML::Document.new result_info # 转换成xml文档
locations_eles = doc.get_elements("type/localtion") # 貌似应该是 location
locations_eles.each do |location|
puts "name is #{location.attributes["name"]}"
puts "id is #{location.attributes["id"]}"
end
end
[/code]
直接写的没运行测试,你试试
这两个要闭合
或者
不闭合 可能会失败rexml要求是标准xml