Welcome to WebmasterWorld Guest from 54.205.170.21

Forum Moderators: open

Message Too Old, No Replies

Parsing XML

Google Geocode API

   
2:02 am on Jun 11, 2013 (gmt 0)

5+ Year Member Top Contributors Of The Month



I don't know if it is because I have a shaky foundation parsing XML or because the Google Geocode API was written by somebody drunk, but I am having a difficult time parsing their XML file and was hoping for some help. I'd like to think it was more of my shaky foundation rather than the drunk coder, but the XML seems strangely nested and because of this I'm having a hard time using online tutorials for parsing XML.

A sample XML file I'm working with:

var url = "http://maps.googleapis.com/maps/api/geocode/xml?latlng=47.616483,-122.319164&sensor=false";

My script so far:

var xml = new XMLHttpRequest();
xml.open("GET", url, false);
xml.send(null);
if(xml.status==200){
...
alert(neighborhood);
}

If available, I'm trying to get the first Long Name for "neighborhood", "sublocality", "locality", and the first Short Name for "country".

I am not using jQuery and do not want to load a huge library for what I hope will be a simple task. If it makes a difference a JSON format is also available by changing xml to json.
5:35 pm on Jun 11, 2013 (gmt 0)

WebmasterWorld Senior Member drdoc is a WebmasterWorld Top Contributor of All Time 10+ Year Member



If using XML, you need to access the data using DOM functions.
I would recommend using JSON instead, as it converts to a JavaScript object.

As for not loading jQuery -- that's fine. But your current AJAX component is insufficient.
I would start here [w3schools.com], going through the next several pages. That way your own AJAX component can be put into its own file for easy reference in multiple projects.
 

Featured Threads

Hot Threads This Week

Hot Threads This Month