XPath will be a big help when parsing XML. Looks like hpricot has support for it, so it's incredibly easy.
The XPath expression to extract the data
attribute inside a city
element is as follows:
/forecast_information/city/@data
The expression says, find the attribute named data
(that's what the @
sign means) inside the element named city
, which is in turn inside the element named forecast_information
.
Now, the XML you linked on google.ru is more complicated than the example you posted here. To extract the same information from it, use this expression:
//city/@data
This expression says, find the attribute named data
inside the element named city
, no matter where city
is in the source XML.