Reputation: 8348
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
Upvotes: 2
Views: 3892
Reputation: 389
what about to use the json service and not to parse any xml? try this to understand what I mean.
Upvotes: 0
Reputation: 497
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;
}
Upvotes: 4
Reputation: 2721
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
>>>
Upvotes: 1
Reputation: 342413
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
Upvotes: 0