0

rexml Xpathを使用してXMLを解析しようとしていますが、const_missing:rhomobileアプリケーションのXPathとしてエラーが発生しています。誰かが私に解決策を与えることができますか?

以下にサンプルコードを示します。file=File.new(file_name)begin require'rexml / document'

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

2 に答える 2

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

rhoxmlがrexmlではなくbuild.ymlファイルに設定されていることを確認してください。これは正常に機能し、少し高速です

于 2011-07-15T20:10:08.643 に答える
0

build.yml ファイルで:

extensions:
- rexml 

ブラックベリーを使用している場合は、rexml を rhoxml に置き換えます

これを行ったと仮定すると、XPath を次のように置き換えます。

REXML::XPath.match(xmldoc, "//MP_HOST_NAME" )
于 2011-06-30T14:39:28.890 に答える