SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Addict michael - ohio's Avatar
    Join Date
    Dec 2004
    Location
    ohio
    Posts
    257
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help updating asp/vbscript to parse XML webservice results

    (cross posted from XML/WebService forum - 147+ views with no replies)

    The webservice I was using to get address and geo-code data has changed their XML results format and label/node names.
    I can't seem to get the this code to properly parse/read the new nodes (likely because I don't know how to properly identify the node name/child.node name).

    Any help would be greatly appreciated.

    Notes:
    The function that calls the webservice is named GeoCodeFunction
    The IF/THEN to check for 'precision' needs to be changed to a more simple check for 'quality' > 85
    The ZipCode variable needs to now = XML reslut: 'uzip'
    The ZipCodePlusFour variable needs to now = XML result: 'postal'
    You'll see a funtion named GetRecordSet in the code - it query's the DB


    I would be DELIGHTED to pay someone to help me with this...
    but I'm also happy to try to learn from you folks how to properly read XML node results into variables, and do it here so that others can follow and learn from my ignorance.

    Thanks.

    - Michael

    ------------------------------------------------------------------
    This asp page (below) is called after an HTML form submission
    which writes user data to the DB and:

    a) opens a DB connection, checks a website visitor's login status, gets their userID
    b) retreives user's data (address) from a MySQL DB table, name: 'users'
    c) if exists, sends the address data to a webservice (external function: 'geocode')
    d) reads the returned XML results
    e) updates the database table with the new address data
    f) redirects the user to another html/asp page


    ------------------------------------------------------------------
    Code:
    <!--#include file="./include/functions.asp"-->
    
    <%
    call openDB
    call checkLogin
    if not session("userType") = "P" then response.redirect "sitter_main.asp"
    idUser=session("idUser")
     
    set oUser=getRecordSet("SELECT * FROM Users WHERE idUser=" & idUser)
     
    if not isnull(oUser("zipCode")) and not isEmpty(oUser("zipCode")) then
     on error resume next
     parsedXML=GeoCode(oUser("streetAddress"), oUser("city"), oUser("state"), oUser("zipCode"))
     on error goto 0
     
    	if err.number=0 and Len(ParseTag("Latitude",parsedXML)) > 0 then
    		if instr(1,parsedXML,"precision=""address""") > 1 or instr(1,parsedXML,"precision=""street""") > 1 then
    			ExecuteSQL "Update Users SET " & _
    				"streetaddress=""" & ParseTag("Address",parsedXML) & """," & _
    				"city=""" & ParseTag("City",parsedXML) & """," & _
    				"zipCode=""" & Left(ParseTag("Zip",parsedXML),5) & """," & _
    				"zipCodePlusFour=""" & ParseTag("Zip",parsedXML) & """," & _
    				"Latitude=" & ParseTag("Latitude",parsedXML) & "," & _
    				"Longitude=" & ParseTag("Longitude",parsedXML) & "," & _
    				"`status`=""A""," & _
    				"Comments="""" " & _
    				"WHERE idUser=" & idUser 'active'
    		elseif instr(1,parsedXML,"precision=""zip""") > 1 then
    			ExecuteSQL "Update Users SET " & _
    				"zipCode=""" & Left(ParseTag("Zip",parsedXML),5) & """," & _
    				"zipCodePlusFour=""" & ParseTag("Zip",parsedXML) & """," & _
    				"Latitude=" & ParseTag("Latitude",parsedXML) & "," & _
    				"Longitude=" & ParseTag("Longitude",parsedXML) & "," & _
    				"Comments=""""," & _
    				"`status`=""E"" WHERE idUser=" & idUser 'active'
    		end if
    	else
    		if Len(ParseTag("Zip",parsedXML)) = 0 then
    			ExecuteSQL "Update Users SET " & _
    				"`Comments`=""" & ParseTag("Message",parsedXML) & "<br>" & validSQL(parsedXML,"S") & """," & _
    				"`status`=""E"",Latitude=NULL,Longitude=NULL WHERE idUser=" & idUser
    		else
    			ExecuteSQL "Update Users SET " & _
    				"streetaddress=""" & ParseTag("Address",parsedXML) & """," & _
    				"city=""" & ParseTag("City",parsedXML) & """," & _
    				"zipCode=""" & Left(ParseTag("Zip",parsedXML),5) & """," & _
    				"zipCodePlusFour=""" & ParseTag("Zip",parsedXML) & """," & _
    				"`status`=""E"",Latitude=NULL,Longitude=NULL WHERE idUser=" & idUser
    		end if
    	end if
    end if
     
    response.write "<p />"
    response.write parsedXML
    response.redirect "parent_main.asp"
    call closeDB
    %>

    ------------------------------------------------------------------
    These are the new webservice XML results and node names:
    ------------------------------------------------------------------

    <?xml version="1.0" encoding="UTF-8" ?>
    - <ResultSet version="1.0">
    <Error>0</Error>
    <ErrorMessage>No error</ErrorMessage>
    <Quality>87</Quality> (replacement for 'precision - different values!)
    - <Result>
    <quality>87</quality> (was 'precision' - different values!)
    <latitude>41.362040</latitude> (was 'Latitude')
    <longitude>-81.359281</longitude> (was 'Longitude')
    <line1>1234 Any St</line1> (was 'Address')
    <postal>44123-1234</postal> (was 'Zip')
    <city>Any Town</city> (was 'City')
    <uzip>44123</uzip> (was 'Zip' - truncated, left, 5)
    </Result>
    </ResultSet>

    ------------------------------------------------------------------
    And here is the external function that submits to the webservice:
    ------------------------------------------------------------------

    Code:
    Function GeoCode(street, city, state, zipcode)
     
        set objDocXML = Server.CreateObject("MSXML2.DomDocument")
        set objXMLHTTP = Server.CreateObject("Msxml2.XMLHTTP.3.0")
        set objDocXML = Server.CreateObject("MSXML2.DOMDocument")
        Dim XMLresponse,i
        Dim webXML
        Dim webService
     
        Dim ITSArray,ITSReturn
     
        webService = "http://where.yahooapis.com/geocode?&appid=[appid]?&location="&street="+street+"&city="+city+"&state="+state+"&zip="+zipcode"
     
        objXMLHTTP.Open "post", webService, False
     
        objXMLHTTP.send payload
        XMLresponse = objXMLHTTP.responseText
     
        objDocXML.loadXML (XMLresponse)
     
    	GeoCode=objDocXML.xml
     
     
    End Function
    ------------------------------------------------------------------
    Correct Mapping of new XML nodes to Database Fields:
    ------------------------------------------------------------------


    XML RESULT Database Field
    --------------------------------------------
    <quality> = (does not exist)
    <line1> = streetAddress
    <city> = city
    <statecode> = state
    <uzip> = zipCode
    <postal> = zipCodePlusFour
    <latitude> = Latitude
    <longitude> = Longitude
    <ErrorMessage> = comments



    ------------------------------------------------------------------
    Detailed information on the webservice can be found here:
    http://developer.yahoo.com/geo/placefinder/
    ------------------------------------------------------------------

  2. #2
    SitePoint Enthusiast
    Join Date
    Jan 2012
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What does the "ParseTag" function look like ?

  3. #3
    SitePoint Enthusiast
    Join Date
    Jan 2012
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this,

    Code:
    	if err.number=0 and Len(ParseTag("latitude",parsedXML)) > 0 then
    		if Cint(parsetag("quality",parsedXML))>=87 then
    			ExecuteSQL "Update Users SET " & _
    				"streetaddress=""" & ParseTag("line1",parsedXML) & """," & _
    				"city=""" & ParseTag("city",parsedXML) & """," & _
    				"zipCode=""" & ParseTag("uzip",parsedXML) & """," & _
    				"zipCodePlusFour=""" & ParseTag("postal",parsedXML) & """," & _
    				"Latitude=" & ParseTag("latitude",parsedXML) & "," & _
    				"Longitude=" & ParseTag("longitude",parsedXML) & "," & _
    				"`status`=""A""," & _
    				"Comments="""" " & _
    				"WHERE idUser=" & idUser 'active'
    		elseif Cint(parsetag("quality",parsedXML))>=60 then
    			ExecuteSQL "Update Users SET " & _
    				"zipCode=""" & Left(ParseTag("uzip",parsedXML),5) & """," & _
    				"zipCodePlusFour=""" & ParseTag("postal",parsedXML) & """," & _
    				"Latitude=" & ParseTag("latitude",parsedXML) & "," & _
    				"Longitude=" & ParseTag("longitude",parsedXML) & "," & _
    				"Comments=""""," & _
    				"`status`=""E"" WHERE idUser=" & idUser 'active'
    		end if
    	else
    		if Len(ParseTag("uzip",parsedXML)) = 0 then
    			ExecuteSQL "Update Users SET " & _
    				"`Comments`=""" & ParseTag("ErrorMessage",parsedXML) & "<br>" & validSQL(parsedXML,"S") & """," & _
    				"`status`=""E"",Latitude=NULL,Longitude=NULL WHERE idUser=" & idUser
    		else
    			ExecuteSQL "Update Users SET " & _
    				"streetaddress=""" & ParseTag("line1",parsedXML) & """," & _
    				"city=""" & ParseTag("city",parsedXML) & """," & _
    				"zipCode=""" & ParseTag("uzip",parsedXML) & """," & _
    				"zipCodePlusFour=""" & ParseTag("postal",parsedXML) & """," & _
    				"`status`=""E"",Latitude=NULL,Longitude=NULL WHERE idUser=" & idUser
    		end if
    	end if

    I have changed the check for "precision='zip'" to check for the value 60 for quality, that's what i could make out from the docs. If it is not, you will have to change that.

    wanted to see the parseTag function to see how it was getting the First Level Node texts, ErrorMessage is a First Level node, while the others are all children of Result....

    Post if you have problems.

  4. #4
    SitePoint Addict michael - ohio's Avatar
    Join Date
    Dec 2004
    Location
    ohio
    Posts
    257
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Flakes View Post
    What does the "ParseTag" function look like ?
    '*********************************************************************
    'parsing
    '*********************************************************************
    Code:
    Function ParseTag( tag ,  rsp )
    	Dim sb
    	Dim idxSt, idxEnd 'As Integer
    
    	rsp = rsp
    
    	sb = "<" & tag & ">"
    	idxSt = -1
    	idxEnd = -1
    
    	idxSt = InStr(rsp,sb)
    	If 0 = idxSt Then
    		ParseTag = ""
    		Exit Function
    	End If
    
    	idxSt = idxSt + Len(sb)
    	sb = "</" & tag & ">"
    	idxEnd = InStr(idxSt, rsp,sb)
    
    	If 0 = idxEnd Then
    	   ParseTag = ""
    	   Exit Function
    	End If
    
    	ParseTag = trim(Mid(rsp, idxSt, (idxEnd - idxSt)))
    
    End Function

  5. #5
    SitePoint Addict michael - ohio's Avatar
    Join Date
    Dec 2004
    Location
    ohio
    Posts
    257
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Flakes View Post
    Try this,
    That executed without any syntax error (once I added the last endif), but no fields in the DB were updated.


    Thank you for tackling this with me. I really appreciate it. - m

  6. #6
    SitePoint Enthusiast
    Join Date
    Jan 2012
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You will have to debug a bit and see which part of the if checking the flow is going through. put some response.write at different places in the code and hide the response.redirect part to see the output from the response.write

    Code:
    	if err.number=0 and Len(ParseTag("latitude",parsedXML)) > 0 then
    Response.write "<br /> In the first check for latitude" 
    		if Cint(parsetag("quality",parsedXML))>=87 then
    			strSQL= "Update Users SET " & _
    				"streetaddress=""" & ParseTag("line1",parsedXML) & """," & _
    				"city=""" & ParseTag("city",parsedXML) & """," & _
    				"zipCode=""" & ParseTag("uzip",parsedXML) & """," & _
    				"zipCodePlusFour=""" & ParseTag("postal",parsedXML) & """," & _
    				"Latitude=" & ParseTag("latitude",parsedXML) & "," & _
    				"Longitude=" & ParseTag("longitude",parsedXML) & "," & _
    				"`status`=""A""," & _
    				"Comments="""" " & _
    				"WHERE idUser=" & idUser 'active'
     Response.write "<br> strSQL="&strSQL&"<br>"
                            ExecuteSQL strSQL
    		elseif Cint(parsetag("quality",parsedXML))>=60 then
    			strSQL= "Update Users SET " & _
    				"zipCode=""" & Left(ParseTag("uzip",parsedXML),5) & """," & _
    				"zipCodePlusFour=""" & ParseTag("postal",parsedXML) & """," & _
    				"Latitude=" & ParseTag("latitude",parsedXML) & "," & _
    				"Longitude=" & ParseTag("longitude",parsedXML) & "," & _
    				"Comments=""""," & _
    				"`status`=""E"" WHERE idUser=" & idUser 'active'
    Response.write "<br> strSQL="&strSQL&"<br>"
                            ExecuteSQL strSQL		
    
    end if
    	else
    		if Len(ParseTag("uzip",parsedXML)) = 0 then
    			strSQL="Update Users SET " & _
    				"`Comments`=""" & ParseTag("ErrorMessage",parsedXML) & "<br>" & validSQL(parsedXML,"S") & """," & _
    				"`status`=""E"",Latitude=NULL,Longitude=NULL WHERE idUser=" & idUser
    		
    Response.write "<br> strSQL="&strSQL&"<br>"
                            ExecuteSQL strSQL
    else
    			strSQL="Update Users SET " & _
    				"streetaddress=""" & ParseTag("line1",parsedXML) & """," & _
    				"city=""" & ParseTag("city",parsedXML) & """," & _
    				"zipCode=""" & ParseTag("uzip",parsedXML) & """," & _
    				"zipCodePlusFour=""" & ParseTag("postal",parsedXML) & """," & _
    				"`status`=""E"",Latitude=NULL,Longitude=NULL WHERE idUser=" & idUser
    Response.write "<br> strSQL="&strSQL&"<br>"
                            ExecuteSQL strSQL		
    end if
    	end if

    Remember, You will have to comment out the Response.redirect part to see the output.

  7. #7
    SitePoint Addict michael - ohio's Avatar
    Join Date
    Dec 2004
    Location
    ohio
    Posts
    257
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks - I'm playing with it to try to see what's going on.

    - Michael

    Quote Originally Posted by Flakes View Post
    You will have to debug a bit and see which part of the if checking the flow is going through. put some response.write at different places in the code and hide the response.redirect part to see the output from the response.write

    Remember, You will have to comment out the Response.redirect part to see the output.


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •