tags:

views:

476

answers:

1

My xml:

http://www.google.ru/ig/api?weather=Chelyabinsk

<forecast_information>
  <city data="Chelyabinsk, Province of Chelyabinsk"/>
</forecast_information>

How to get city data for example? Not inner_html, just attributes like city data, postal code etc.

A: 

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.

Welbog