On Oct 7, 6:01 pm, Becca Girl <[EMAIL PROTECTED]>
wrote:
> Hi.
>
> I've got a file that contains a table that looks like this:
>
> <table>
>   <tr><td>column title a</td><td>column title b</td></tr>
>   <tr><td>row 1 a</td><td>row 1 b</td></tr>
>   <tr><td>row 2 a</td><td>row 2 b</td></tr>
>   <tr><td>row 3 a</td><td>row 3 b</td></tr>
>   <tr><td>row 4 a</td><td>row 4 b</td></tr>
> </table>
>
> I need to read the rows starting with the second table row, which
> excludes the title of the column.  Then I need to read each column's
> value.
>
> How can I loop through each row to get each value that I need?

Who needs loops when you have XPath? You can grab an entire column in
one fell swoop.

  require 'xml'
  doc = XML::Parser.string(html).parse
  column1 = doc.find('/table/tr[position()>1]/td[1]/text()')
  puts column1.to_a

You'll need libxml for that (gem install libxml-ruby). Hpricot is not
XPath compliant enough.

See my XPath article here:
http://markthomas.org/2008/08/22/improve-your-XML-parsing-with-XPath/

-- Mark.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to