put into variable javascript

Posted on 2006-04-25
Last Modified: 2008-02-01
hello, i have this xml file

I need to be able to get the longitude and latitude into a variable using javascript.  I need it for an asp page.  thanks.
Question by:christampa
    LVL 3

    Author Comment

    isnt there any xml experts on this site????
    LVL 15

    Assisted Solution

    Here is a sample page I have put together for you.  It is based on the example from W3schools:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">


    var xmlHttp;

    function alertLatLong()
    var url="";
    xmlHttp=GetXmlHttpObject(stateChanged);"GET", url , true);

    function stateChanged()
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
    var xml = xmlHttp.responseXML
    var lat = xml.selectSingleNode("//Latitude").text;
    var long = xml.selectSingleNode("//Longitude").text;
    alert("Latitude: " + lat + ", Longitude: " + long);

    function GetXmlHttpObject(handler)
    var objXmlHttp=null;

    if (navigator.userAgent.indexOf("Opera")>=0)
    alert("This example doesn't work in Opera");
    if (navigator.userAgent.indexOf("MSIE")>=0)
    var strName="Msxml2.XMLHTTP";
    if (navigator.appVersion.indexOf("MSIE 5.5")>=0)
    objXmlHttp=new ActiveXObject(strName);
    return objXmlHttp;
    alert("Error. Scripting for ActiveX might be disabled");
    if (navigator.userAgent.indexOf("Mozilla")>=0)
    objXmlHttp=new XMLHttpRequest();
    return objXmlHttp;


    <body onload="alertLatLong();">

    LVL 60

    Accepted Solution

    Hi christampa,
    > isnt there any xml experts on this site????

    plenty, but they all have jobs outside Experts Exchange

    here is what you need, asuming you have msxml4 installed

                     var oXML = new ActiveXObject("Msxml2.XMLHTTP.4.0");
           "GET", "", false);
                     var oRes = oXML.responseXML;
                     var oLat = oRes.selectSingleNode("//*[local-name() = 'Latitude']").text;
                     var oLong = oRes.selectSingleNode("//*[local-name() = 'Longitude']").text;
    you can also use the XML server http object, it is just a matter of thread safety

    simple get to get the XML object
    and two simple selectSingleNode to get the elements you want

    LVL 3

    Author Comment

    hahahaha they both worked!!!!!!!!!!!! thank you thank you!!!

    how can i give you both 500 points?
    LVL 60

    Expert Comment

    by:Geert Bormans
    >how can i give you both 500 points?

    glad it worked,
    you can't give twice 500 pts,
    but you can split points if you want (push that button at the bottom)
    or give points to mshogren for being first

    LVL 3

    Author Comment

    yours actually worked slightly better because mshogren answer asked me if i wanted to accept it getting information from another web site.  yours didnt.  is there a reason for that?
    LVL 60

    Expert Comment

    by:Geert Bormans
    Mshogrens code is set up for a browser as AJAX code
    - he (apologies if you are not a "he", ms) copied the code for browsers, giving some browser detection code for choosing the correct XML HTTP object
    (I did not do that since you asked for ASP code, so I assume you are doing that server side, Windows)
    - he set the code up for asynchronous XML retrieval..."GET", url , true);
    the "true" makes that happen
    this means that you do the request, but continue with the rest of your code
    if the respons comes in, the readystate changes and the function stateChanged() is launched
    I think that this raises the extra request for acceptance (activeX in the browser)
    I set that third parameter to false, this means synchronous, so the code waits until the XML comes in
    you can do error reporting based on the object having content or not after the request

    hope this helps

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Suggested Solutions

    Title # Comments Views Activity
    Xsl Nested Loop 4 18
    XML and javascript 14 75
    VB.NET WEb Service complex type 14 45
    Unattended XML settings 4 56
    The Problem How to write an Xquery that works like a SQL outer join, providing placeholders for absent data on the outer side?  I give a bit more background at the end. The situation expressed as relational data Let’s work through this.  I’ve …
    Introduction In my previous article ( I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
    This video discusses moving either the default database or any database to a new volume.
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now