views:

361

answers:

2

Hi,

I'm trying to extract some information from an online xml weather resource (weather underground).

I am able to open the resource and pull out the desired elements, but what I really want is to return the element text as a variable, without the containing xml element tags, so I can manipulate it and display it on a web page.

Perhaps there is a way to do this using regexp to strip off the tags, but I suspect (/hope) I can do this in a more elegant fashion directly in nokogiri...

Currently I am using irb to work out the syntax:

irb>require 'rubygems'
irb>require 'nokogiri'
irb>require 'open-uri'
irb>doc = Nokogiri::XML(open('http://api.wunderground.com/auto/wui/geo/WXCurrentObXML/index.xml?query=KBHB'))
=> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"&gt;
=> <?xml version="1.0"?>
# [...]
<!-- 0.036:0 -->

irb>doc.xpath('/current_observation/weather')
=> <weather>Clear</weather>irb(main):019:0> 
irb>doc.xpath('/current_observation/wind_dir')
=> <wind_dir>North</wind_dir>
irb>doc.xpath('/current_observation/wind_mph')
=> <wind_mph>10</wind_mph>
irb>doc.xpath('/current_observation/pressure_string')
=> <pressure_string>31.10 in (1053 mb)</pressure_string>

I need help with the specific syntax. Using constructs such as

doc.xpath.element('/current_observation/weather')
doc.xpath.text('/current_observation/weather')
doc.xpath.node('/current_observation/weather')
doc.xpath.element.text('/current_observation/weather')

, etc. all return errors.

Thanks!

A: 

Something like this works for me:

irb(main):019:0> doc.xpath('//current_observation/weather').first.content
=> "Clear"
ire_and_curses
+1  A: 

As per XPath, you can return the text node of an element with text().

In your example it should be doc.xpath('/current_observation/weather/text()') to get the content of weather's text node.

kRON
+1: This works.
ire_and_curses
kRON yes, thanks that's exactly it.I also got doc.xpath('/current_observation/weather').text() to work, but it returned quote marks around the string. I think your way is probably more useful to me. Thanks.
David Smith
Yes this works. I also tried doc.xpath('/current_observation/weather').text() -- but it returns quotes around the string. Your way is probably more useful to me. Thanks
David Smith
OK I'm confused. When I leave the page and return my comment disappeared and I thought I had confused preview with post...
David Smith