在 Rhomobile 中使用 Xpath 时出错

发布于 2024-11-14 01:00:27 字数 281 浏览 7 评论 0原文

我正在尝试使用 rexml Xpath 解析 XML,但在 rhomobile 应用程序中面临 const_missing: XPath 错误。谁能给我解决方案。

下面是示例代码: 文件 = File.new(文件名) 开始 需要“rexml/文档”

      xmldoc = REXML::Document.new(file)
      names = XPath.match(xmldoc, "//MP_HOST_NAME" )

I am trying to parse XML using rexml Xpath, but facing error as const_missing: XPath in rhomobile application. can anyone give me the solution.

Below is the sample code:
file = File.new(file_name)
begin
require 'rexml/document'

      xmldoc = REXML::Document.new(file)
      names = XPath.match(xmldoc, "//MP_HOST_NAME" )

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ヤ经典坏疍 2024-11-21 01:00:27

在您的 build.yml 文件中:

extensions:
- rexml 

如果使用黑莓,请将 rexml 替换为 rhoxml

假设您已完成此操作,请将您的 XPath 替换为:

REXML::XPath.match(xmldoc, "//MP_HOST_NAME" )

in your build.yml file:

extensions:
- rexml 

if using blackberry, replace rexml with rhoxml

Assuming you've done this, replace your XPath with:

REXML::XPath.match(xmldoc, "//MP_HOST_NAME" )
红墙和绿瓦 2024-11-21 01:00:27

这是我敲来测试 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,这样效果很好,而且速度更快

Here is a sample controller I knocked to test xml parsing
I can use the get_names method in the view then to get an array of 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

ensure that rhoxml is set in the build.yml file rather than rexml this works fine and it's a little faster

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文