[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

put into variable javascript

Posted on 2006-04-25
7
Medium Priority
?
263 Views
Last Modified: 2008-02-01
hello, i have this xml file

http://api.local.yahoo.com/MapsService/V1/geocode?appid=yahoo&street=702+First+ave&city=Sunnyvale&state=CA

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

Author Comment

by:christampa
ID: 16534503
isnt there any xml experts on this site????
0
 
LVL 15

Assisted Solution

by:mshogren
mshogren earned 1000 total points
ID: 16535241
Here is a sample page I have put together for you.  It is based on the example from W3schools:
http://www.w3schools.com/ajax/default.asp

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Test</title>

<script>

var xmlHttp;

function alertLatLong()
{
var url="http://api.local.yahoo.com/MapsService/V1/geocode?appid=yahoo&street=702+First+ave&city=Sunnyvale&state=CA";
xmlHttp=GetXmlHttpObject(stateChanged);
xmlHttp.open("GET", url , true);
xmlHttp.send(null);
}

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");
return;
}
if (navigator.userAgent.indexOf("MSIE")>=0)
{
var strName="Msxml2.XMLHTTP";
if (navigator.appVersion.indexOf("MSIE 5.5")>=0)
{
strName="Microsoft.XMLHTTP";
}
try
{
objXmlHttp=new ActiveXObject(strName);
objXmlHttp.onreadystatechange=handler;
return objXmlHttp;
}
catch(e)
{
alert("Error. Scripting for ActiveX might be disabled");
return;
}
}
if (navigator.userAgent.indexOf("Mozilla")>=0)
{
objXmlHttp=new XMLHttpRequest();
objXmlHttp.onload=handler;
objXmlHttp.onerror=handler;
return objXmlHttp;
}
}
</script>

</head>

<body onload="alertLatLong();">


</body>
</html>
0
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 1000 total points
ID: 16535337
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");
                 oXML.open("GET", "http://api.local.yahoo.com/MapsService/V1/geocode?appid=yahoo&street=702+First+ave&city=Sunnyvale&state=CA", false);
                 oXML.send();
                 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

Cheers!
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 3

Author Comment

by:christampa
ID: 16535509
hahahaha they both worked!!!!!!!!!!!! thank you thank you!!!

how can i give you both 500 points?
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 16535583
>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

cheers
0
 
LVL 3

Author Comment

by:christampa
ID: 16535687
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?
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 16536587
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... xmlHttp.open("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
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
Integration Management Part 2
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

873 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