tags:

views:

139

answers:

2

Hi there, I've got a really simple xml doc (extracted from an html table), and a really simple Nokogiri script, but for some reason I can't get the text out of the xml nodes. I can get attributes, but not the text/content. Anyone have any idea what could be wrong with the following?

Here's the xml:

<?xml version="1.0" encoding="UTF-8"?>
<table class="renderedtable" border="1" cellpadding="2" cellspacing="0" width="100%">
  <tr>
    <th valign="top">  
  <p class="MsoNormal"> AIR CONDITIONING, VENTILATION &amp; HEATING</p>
    </th>
  </tr> 
</table>

And the simplest script in the world that raises a "undefined method 'content'"

require 'nokogiri'

a = IO.read('services_table.xml')

reader = Nokogiri::XML::Reader(a)

reader.each do |node|

  if node.name == 'p'

    @category = node.content

  end
end
A: 

Edit: Got to check the Nokogiri doc. With node.methods you will see there's a method named value, it seems to be the one to use instead of content.

Yoann Le Touche
No dice. Value returns nil :-/...yup, I tried node.methods.sort, looked through all the availabe ones and tried a bunch.
btelles
+3  A: 

Try

reader.each { |node|
  p node.inner_xml if node.name=="p"
}

Nokogiri.each returns instances of Nokogiri::XML::Reader, not Nokogiri::XML::Node

kirushik
Tada!!! Thank you Mr. (or Ms.)Kirushik.
btelles
Mr.Kirushik is derived from Kirill (Кирилл) - it's common Russian name for males. Both names are still underlined in Firefox; that gives me a tip about being not-that-common name someplaces abroad.You're welcome.
kirushik
Thanks, this worked great. But I'm curious as to why this isn't in the docs?
Bob
Never mind, I found the relevant docs.
Bob