我正在尝试使用rexml Xpath解析XML,但在rhomobile应用程序中面临const_missing:XPath的错误。任何人都可以给我解决方案。
以下是示例代码: file = File.new(file_name) 开始 要求'rexml / document'
xmldoc = REXML::Document.new(file)
names = XPath.match(xmldoc, "//MP_HOST_NAME" )
答案 0 :(得分:0)
:
extensions:
- rexml
如果使用黑莓,请用rhoxml替换rexml
假设您已完成此操作,请将您的XPath替换为:
REXML::XPath.match(xmldoc, "//MP_HOST_NAME" )
答案 1 :(得分:0)
这是我为测试xml解析而敲门的示例控制器 我可以在视图中使用get_names方法然后获取名称数组
require 'rho/rhocontroller'
require 'rexml/document'
class WebServiceTestController < Rho::RhoController
def index
@@get_result = ""
Rho::AsyncHttp.get(
:url => 'http://www.somesite.com/some_names.xml',
#:authorization => {:type => :basic, :username => 'user', :password => 'none'},
:callback => (url_for :action => :httpget_callback),
:authentication => {
:type => :basic,
:username => "xxxx",
:password => "xxxx"
},
:callback_param => "" )
render :action => :wait
end
def get_res
@@get_result
end
def get_error
@@error_params
end
def httpget_callback
if @params["status"] != "ok"
@@error_params = @params
WebView.navigate( url_for(:action => :show_error) )
else
@@get_result = @params["body"]
begin
# require "rexml/document"
@@doc = REXML::Document.new(@@get_result)
# puts "doc : #{doc}"
rescue Exception => e
# puts "Error: #{e}"
@@get_result = "Error: #{e}"
end
WebView.navigate( url_for(:action => :show_result) )
end
end
def show_error
render :action => :error, :back => '/app'
end
def show_result
render :action => :index, :back => "/app"
end
def get_doc
@@doc
end
def get_names
names = []
REXML::XPath.each( get_doc, "//name_or_whatever_you_are_looking_for") do |element|
names << element.text
end
names
end
end
确保在build.yml文件中设置了rhoxml而不是rexml,这样可以正常工作并且速度更快