tags:

views:

229

answers:

3

Hi,

i have a google map link which returns a xml to me. I want values of a specific tag from that xml. can some one suggest me how will i do it. Link:

http://maps.google.com/maps/api/geocode/xml?address=1270 Broadway Ste 803, New York, NY 10001, USA&sensor=false
+1  A: 

depends what language you want to use. if you're developing with python, you can use python's minidom parser.

An example of how this can be parsed in python:

>>> import urllib
>>> from xml.dom import minidom
>>> URL="http://maps.google.com/maps/api/geocode/xml?address=1270%20Broadway%20Ste%20803,%20New%20York,%20NY%2010001,%20USA&sensor=false"
>>> loc = dom.getElementsByTagName('location')[0]
>>> lat = loc.getElementsByTagName('lat')[0].firstChild.data
>>> lng = loc.getElementsByTagName('lng')[0].firstChild.data
>>> print lat, lng
40.7486930 -73.9877870
>>> 
pulegium
i am using asp.net, sorry i forget to mention that in the post, can u help me out in asp.net
pankaj
no idea tbh... try google, here's one example i found at least readable: http://www.daniweb.com/forums/thread30083.htmlhaven't used windows for ages...
pulegium
A: 

if you are on *nix and you just want to see the values of specific tags, regardless of where they are in the hierarchy,

$ s="http://maps.google.com/maps/api/geocode/xml?address=1270 Broadway Ste 803, New York, NY 10001, USA&sensor=false"
$ wget -O- -q "$s" | awk '/<lat>|<lng>/' # getting </lat> tags and <lng> tags
ghostdog74
+2  A: 

Here's a function that will return the coordinates in a dictionary. Note: this is with the new V3 api. No API key required. sensor is.

    // Figure out the geocoordinates for the location
private Dictionary<string, string> GeoCode(string location)
{
    // Initialize the dictionary with empty values.
    Dictionary<string, string> dic = new Dictionary<string, string>();
    dic.Add("latitude", "");
    dic.Add("longitude", "");

    try
    {
        string url = "http://maps.google.com/maps/api/geocode/xml?address=" + System.Web.HttpUtility.UrlEncode(location) + "&sensor=false";
        XmlDocument doc = new XmlDocument();
        doc.Load(url);
        XmlNodeList nodes = doc.SelectNodes("//location");

        // We're assuming there's only one location node in the xml.
        foreach (XmlNode node in nodes)
        {
            dic["latitude"] = node.SelectSingleNode("lat").InnerText;
            dic["longitude"] = node.SelectSingleNode("lng").InnerText;
        }
    }
    catch (Exception)
    {
        // If anything goes wrong, we want to get empty values back.
        dic["latitude"] = "";
        dic["longitude"] = "";
    }

    return dic;
}