Staging copy. The live site is highearthorbit.com.

Apr 23, 2006 · Programming

Javascript XML parsing for GML and you

You find the need to determine the locations of your visitors, and you wonder how to do this? Surely it's impossible. Alas, it is not. Services like HostIP provide the ability to locate your users given their IP address. The slick way to do this is via javascript. Unfortunately, the HostIP API doesn't provide a JSON interface.

But of course, the response is in XML, so you're also wondering how to parse this XML response for something meaningful? The answer is the DOMParser and namespaces. There is a really nice function, getElementsByTagNameNS that allows you to parse out elements given a namespace. I kept trying getElementsByTagNameNS("gml","name"), which didn't work. The answer seems to be that you need to include a valid uri for the namespace.

var dom = new DOMParser ( ) .parseFromString ( responseText,
"application/xml" ) ;

var location = dom.getElementsByTagNameNS (
"http://www.opengis.net/gml", "name" ) [1].firstChild.data
if location == " ( Unknown city ) "
return "";


Programming · Geolocation · Javascript · Howto