Status
door is becoming closed so people stop coming in
Location
Herndon, VA
Subscribe to GeoRSS Subscribe to KML


Javascript XML parsing for GML and you

Published in Geolocation, Howto, Javascript, Programming  |  3 Comments


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 "";

Similar Posts


Responses

  1. Cesar says:

    May 10th, 2006 at 3:07 am (#)

    Hello,

    I’m spanish student, and I would like how to integrate GML in a JavaScript file.

    Thanks
    Regards,

    César

  2. Andrew says:

    May 10th, 2006 at 9:06 am (#)

    @César - perhaps a little more information on what you’re trying to do?

  3. Scooper says:

    April 15th, 2007 at 1:40 am (#)

    Andrew,
    The JavaScript security model does not allow XMLHttpRequest to get the XML from website other than the one hosting the JavaScript containing the XMLHttpRequest. The DOMParser.getElementsByTagNameNS method is nice, but in this case useless, because it can’t see the XML from the HostIP website. Or do you have a workaround?

Leave a Response