Avatar of Kristen Jones
Kristen Jones
Flag for United States of America asked on

Parsing XML with classic ASP

I need to be able to separate child nodes and apply logic

my XML Source: https://vpic.nhtsa.dot.gov/api/vehicles/decodevin/ZPBUA1ZLXKLA00941?format=xml

For some reason the text values are concatenating but I want them to be broken out by the Node name and value.  I put notes in the code.  The Code DOES work just not how I need it.

  
<%

url = "https://vpic.nhtsa.dot.gov/api/vehicles/decodevin/ZPBUA1ZLXKLA00941?format=xml"
     
     ' add a BASE HREF tag
     Dim xmldoc
     dim xmlhttp 
      
     Response.write "<BASE HREF='" & url & "'><P>" & url  & "<P>"
    
     
     set xmlhttp = CreateObject("Microsoft.XMLHTTP")
     Set xmlDoc = CreateObject("Microsoft.XMLDOM") 
     xmlhttp.open "GET", url, false 
     xmlhttp.send ""
          'response.write xmlhttp.responsetext
          
          xmlDoc.async = False
          bresult=xmlDoc.loadxml(xmlhttp.responsetext)
       
       State = ""
        ' WORKS ...Set NodeList = xmlDoc.getElementsByTagName("*")
        'Set NodeList = xmlDoc.documentElement.selectNodes("/Response/*")
        'Set NodeList = xmlDoc.documentElement.selectNodes("/Response/Results/*")
   Set NodeList = xmlDoc.documentElement.selectNodes("/Response/Results/DecodedVariable")
        'Set NodeList = xmlDoc.documentElement.selectNodes("/Response/Results/DecodedVariable/*")






        For Each Node in NodeList




         'I NEED SOME SORT OF LOGIC HERE
	           If Node.tagName = "VariableId" Then		        
		        ' response.write Node.tagName & "<br>"	
		        ' Do Something LOGIC	       
	           End If  

           'I NEED SOME SORT OF LOGIC HERE
               'If Node.tagName("VariableId") = 156 then

               'End if






	        response.write "TagName: " &  Node.tagName & "<br>"	  
                response.write("Node.text :" & Node.text & "<br>") 
                response.write("Node.baseName :" & Node.baseName & "<br>") 
                response.write("Node.nodeName:" & Node.nodeName & "<br>") 
                response.write("Node.nodeType:" & Node.nodeType & "<br>") 
                response.write("Node.nodeValue:" & Node.nodeValue & "<br>")
        

	      
        Next
       
      
       set xmlhttp = nothing
%>
 

Open in new window


Current Output snippet
TagName: DecodedVariable
Node.text :142Suggested VIN
Node.baseName :DecodedVariable
Node.nodeName:DecodedVariable
Node.nodeType:1
Node.nodeValue:




TagName: DecodedVariable
Node.text :143Error Code00 - VIN decoded clean. Check Digit (9th position) is correct
Node.baseName :DecodedVariable
Node.nodeName:DecodedVariable
Node.nodeType:1
Node.nodeValue:




TagName: DecodedVariable
Node.text :144Possible Values
Node.baseName :DecodedVariable
Node.nodeName:DecodedVariable
Node.nodeType:1
Node.nodeValue:




TagName: DecodedVariable
Node.text :156Additional Error Text
Node.baseName :DecodedVariable
Node.nodeName:DecodedVariable
Node.nodeType:1
Node.nodeValue:




TagName: DecodedVariable
Node.text :10Destination Market17U.S.
Node.baseName :DecodedVariable
Node.nodeName:DecodedVariable
Node.nodeType:1
Node.nodeValue:




TagName: DecodedVariable
Node.text :26Make502LAMBORGHINI
Node.baseName :DecodedVariable
Node.nodeName:DecodedVariable
Node.nodeType:1
Node.nodeValue:

Open in new window

VB ScriptXMLASP

Avatar of undefined
Last Comment
Kristen Jones

8/22/2022 - Mon
Scott Fell

This worked for me. I adapted from an older solution I have here https://www.experts-exchange.com/questions/27579097/How-to-receive-a-webservice-in-classic-ASP.html

I am accessing the data you are looking for using, node.getElementsByTagName("VariableId").Item(0).Text
  
<%

url = "https://vpic.nhtsa.dot.gov/api/vehicles/decodevin/ZPBUA1ZLXKLA00941?format=xml"
     
     ' add a BASE HREF tag
     Dim xmldoc
     dim xmlhttp 
      
     Response.write "<BASE HREF='" & url & "'><P>" & url  & "<P>"
    
     
     set xmlhttp = CreateObject("Microsoft.XMLHTTP")
     Set xmlDoc = CreateObject("Microsoft.XMLDOM") 
     xmlhttp.open "GET", url, false 
     xmlhttp.send ""
          'response.write xmlhttp.responsetext
          
          xmlDoc.async = False
          bresult=xmlDoc.loadxml(xmlhttp.responsetext)
		  
		  response.write bresult
       
       State = ""
        ' WORKS ...Set NodeList = xmlDoc.getElementsByTagName("*")
        'Set NodeList = xmlDoc.documentElement.selectNodes("/Response/*")
        'Set NodeList = xmlDoc.documentElement.selectNodes("/Response/Results/*")
   Set NodeList = xmlDoc.documentElement.selectNodes("/Response/Results/DecodedVariable")
        'Set NodeList = xmlDoc.documentElement.selectNodes("/Response/Results/DecodedVariable/*")






        For Each Node in NodeList


			response.write "<hr>"

			'I NEED SOME SORT OF LOGIC HERE
	           If Node.tagName = "VariableId" Then		        
		        ' response.write Node.tagName & "<br>"	
		        ' Do Something LOGIC	       
	           End If  

			'I NEED SOME SORT OF LOGIC HERE
               'If Node.tagName("VariableId") = 156 then

               'End if

			response.write node.getElementsByTagName("VariableId").Item(0).Text &"<br>"
			VariableId = node.getElementsByTagName("VariableId").Item(0).Text
			if cint(VariableId) = 156 then 
				response.write "********** FOUND ***********"
			end if

			
			
			

				'response.write "TagName: " &  Node.tagName & "<br>"	  
                'response.write("Node.text :" & Node.text & "<br>") 
                'response.write("Node.baseName :" & Node.baseName & "<br>") 
                'response.write("Node.nodeName:" & Node.nodeName & "<br>") 
                'response.write("Node.nodeType:" & Node.nodeType & "<br>") 
                'response.write("Node.nodeValue:" & Node.nodeValue & "<br>")
				
				response.write "<hr>"
        

	      
        Next
       
      
       set xmlhttp = nothing
%>
 

Open in new window

ste5an

Cause you don't parse the single elements. The .text property does return all text from the node. Thus you need to parse for the single values, e.g. something like

<%
  Sub OutputNode(ANode)
    Response.write "TagName: " &  ANode.tagName & "<br>"
    Response.write("Node.text :" & ANode.text & "<br>")
    Response.write("Node.baseName :" & ANode.baseName & "<br>")
    Response.write("Node.nodeName:" & ANode.nodeName & "<br>")
    Response.write("Node.nodeType:" & ANode.nodeType & "<br>")
    Response.write("Node.nodeValue:" & ANode.nodeValue & "<br>")
  End Sub

  Sub ParseDocument(ADocument)
    For Each Node in ADocument.documentElement.selectNodes("/Response/Results/DecodedVariable")
      OutputNode Node.selectSingleNode("VariableId")
      OutputNode Node.selectSingleNode("Variable")
      OutputNode Node.selectSingleNode("ValueId")
      OutputNode Node.selectSingleNode("Value")
    Next
  End Sub

  Function RetrieveXml(AUrl)
    Dim xmldoc
    Dim xmlhttp

    Response.write "<BASE HREF='" & Aurl & "'><P>" & Aurl  & "<P>"
    Set xmlhttp = CreateObject("Microsoft.XMLHTTP")
    Set xmlDoc = CreateObject("Microsoft.XMLDOM")
    xmlhttp.open "GET", AUrl, false
    xmlhttp.send ""
    xmlDoc.async = False
    xmlDoc.loadxml(xmlhttp.responsetext)
    Set xmlhttp = nothing
    Set RetrieveXml = xmlDoc
  End Function

  Dim url
  Dim document

  url = "https://vpic.nhtsa.dot.gov/api/vehicles/decodevin/ZPBUA1ZLXKLA00941?format=xml"
  document = RetrieveXml(url)
  ParseDocument document
%>

Open in new window

Kristen Jones

ASKER
Thanks Scott, but how do I get the node names and values from the child nodes of DecodedVariable?  for example I can identify the VariableID below at 142 so if my logic find this, then I need to be able to get the Variable value too

<DecodedVariable>
<VariableId>142</VariableId>
<Variable>Suggested VIN</Variable>
<ValueId/>
<Value/>
</DecodedVariable>
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
ASKER CERTIFIED SOLUTION
Scott Fell

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Kristen Jones

ASKER
Thanks guys,  That exactly what I was looking for Scott!