我正在尝试使用 rexml Xpath 解析 XML,但在 rhomobile 应用程序中面临 const_missing: XPath 错误。谁能给我解决方案。
下面是示例代码: file = File.new(file_name) begin require 'rexml/document'
xmldoc = REXML::Document.new(file)
names = XPath.match(xmldoc, "//MP_HOST_NAME" )
我正在尝试使用 rexml Xpath 解析 XML,但在 rhomobile 应用程序中面临 const_missing: XPath 错误。谁能给我解决方案。
下面是示例代码: file = File.new(file_name) begin require 'rexml/document'
xmldoc = REXML::Document.new(file)
names = XPath.match(xmldoc, "//MP_HOST_NAME" )
在您的 build.yml 文件中:
extensions:
- rexml
如果使用黑莓,请将 rexml 替换为 rhoxml
假设您已完成此操作,请将您的 XPath 替换为:
REXML::XPath.match(xmldoc, "//MP_HOST_NAME" )
这是我敲击来测试 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 这可以正常工作并且速度更快一些