0

我的代码:

require 'rexml/document'
require 'rexml/xpath'

doc = REXML::Document.new(response)
REXML::XPath.each(doc, "*//categoryName") { |element| puts element.text }

我希望它返回的是标签内的文本元素......它实际返回的是:

<categoryName> ... </>

关于如何解决这个问题的任何想法?

4

1 回答 1

0

对我来说,一个示例可以正常工作:

require 'rexml/document'
require 'rexml/xpath'

doc = REXML::Document.new("<p>some text <b>this is bold!</b> more text</p>")
REXML::XPath.each(doc, "*//b") { |element| puts element.text }
# >> this is bold!

看另一个例子;

require 'rexml/document'
require 'rexml/xpath'

str = <<-eot
<version>
  <locale name="en-US">
    <title>This is the Title</title>
    <description>this is the description</description>
    <keywords>
      <keyword>this</keyword>
      <keyword>that</keyword>
      <keyword>the other</keyword>
    </keywords>
  </locale>
</version>
eot
doc = REXML::Document.new(str)
REXML::XPath.each(doc, "//title") { |element| puts element.text }
REXML::XPath.each(doc, "//keyword") { |element| puts element.text }
# >> This is the Title
# >> this
# >> that
# >> the other
于 2013-06-26T20:08:18.893 回答