2013-06-26 15 views
0
返回文本

我的代码:XPath不RoR中

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

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

我想这回什么是标签内的文本元素......它实际上返回的是:

<categoryName> ... </> 

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

+0

向我们展示'xml'内容。 –

回答

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