Parsing XML
I'm messing around with a mapping application using the Yahoo online
geocoder.
In my application, the user enters an address search in a form and the
results page loops through the recordset of matching results.
What I want to do is take the address for each matching result and send
it on to the Yahoo geocoder and suck in the latitude and longitude.
So inside my loop, I have this:
<%
url =
"http://api.local.yahoo.com/MapsService/V1/geocode?appid=myy ahooid&street="&rs("location")&chr(38)&"city="&rs("townname" )&chr(38)&"state=NJ"
set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP")
xmlhttp.open "GET", url, false
xmlhttp.send ""
straddress=xmlhttp.responseText
response.write straddress&"<br>"
set xmlhttp = nothing
%>
This code creates a variable straddress for each record that contains
this chunk of XML:
<?xml version="1.0" encoding="UTF-8"?><ResultSet
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="urn:yahoo:maps" xsi:schemaLocation="urn:yahoo:maps
http://api.local.yahoo.com/MapsService/V1/GeocodeResponse.xs d"><Result
precision="address"><Latitude>40.887163</Latitude><Longitude>-74.002151</Longitude><Address>100
MINELL
PL</Address><City>TEANECK</City><State>NJ</State><Zip>07666-5510</Zip><Country>US</Country></Result></ResultSet>
<!-- ws01.search.re2.yahoo.com uncompressed/chunked Tue Feb 7 14:15:56
PST 2006 -->
What's the easiest way to examine that chunk and pull out
strlatitude = 40.887163
strlongitude = -74.002151
TIA...
Re: Parsing XML
set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP")
xmlhttp.open "GET", url, false
xmlhttp.send ""
elemRes = xmlhttp.responseXML.documentElement
stratitude = elemRes.selectSingleNode("Latitude").Text
strlongtitude = elemRes.selectSingleNode("Longitude").Text
Anthony.
Re: Parsing XML
gebelo [at] gmail.com wrote:
> <?xml version="1.0" encoding="UTF-8"?><ResultSet
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> xmlns="urn:yahoo:maps" xsi:schemaLocation="urn:yahoo:maps
> http://api.local.yahoo.com/MapsService/V1/GeocodeResponse.xs d"><Result
> precision="address"><Latitude>40.887163</Latitude><Longitude>-74.002151</Longitude><Address>100
> MINELL
> PL</Address><City>TEANECK</City><State>NJ</State><Zip>07666-5510</Zip><Country>US</Country></Result></ResultSet>
> <!-- ws01.search.re2.yahoo.com uncompressed/chunked Tue Feb 7
> 14:15:56
> PST 2006 -->
>
> What's the easiest way to examine that chunk and pull out
>
> strlatitude = 40.887163
> strlongitude = -74.002151
>
> TIA...
Anthony Jones wrote:
> set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP")
> xmlhttp.open "GET", url, false
> xmlhttp.send ""
> elemRes = xmlhttp.responseXML.documentElement
> stratitude = elemRes.selectSingleNode("Latitude").Text
> strlongtitude = elemRes.selectSingleNode("Longitude").Text
>
> Anthony.
This is perfectly correct. I just want to add that you should add some
error-handling to this, otherwise you could subject your users to cryptic
messages about "something" not being an object.
My preference is to do something like this:
dim elemRes, latNode, longNode
set elemRes = xmlhttp.responseXML.documentElement
set latNode=Nothing
set latNode=elemRes.selectSingleNode("Latitude")
if latNode is Nothing then
'handle situation where call to url returned no latitude
else
stratitude = latNode.nodeValue
end if
Bob Barrows
--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"