• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 265
  • Last Modified:

put into variable javascript

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
christampa
Asked:
christampa
  • 3
  • 3
2 Solutions
 
christampaAuthor Commented:
isnt there any xml experts on this site????
0
 
mshogrenCommented:
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
 
Geert BormansInformation ArchitectCommented:
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
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
christampaAuthor Commented:
hahahaha they both worked!!!!!!!!!!!! thank you thank you!!!

how can i give you both 500 points?
0
 
Geert BormansInformation ArchitectCommented:
>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
 
christampaAuthor Commented:
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
 
Geert BormansInformation ArchitectCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now