0

My code:

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

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

What I want this to return is the text element inside the tag... what it actually returns is:

<categoryName> ... </>

Any idea on how to fix this?

DaveyGravy
  • 21
  • 5

1 Answers1

0

For me with a sample example works fine :

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!

See another example ;

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
Arup Rakshit
  • 116,827
  • 30
  • 260
  • 317