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

LVL 1
Jason JonesGIS/APP DevAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
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

ste5anSenior DeveloperCommented:
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

Jason JonesGIS/APP DevAuthor Commented:
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>
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
ste5an is correct and his example is more elegant using functions and subroutines. It did have a couple of errors when I ran it. Using your code with some slight modifications as noted by ste5an, this example will output only when the id is less than 142.


  
<%

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 ""
       
          xmlDoc.async = False
         xmlDoc.loadxml(xmlhttp.responsetext)

      Set NodeList = xmlDoc.documentElement.selectNodes("/Response/Results/DecodedVariable")
	

        For Each Node in NodeList


				if NOT Node.selectSingleNode("VariableId") is nothing then
					VariableId = Node.selectSingleNode("VariableId").text
					else
					VariableId = ""
				end if
				if NOT Node.selectSingleNode("Variable") is nothing then
					Variable = Node.selectSingleNode("Variable").text
					else
					Variable = ""
				end if
				if NOT Node.selectSingleNode("ValueId") is nothing then
					ValueId = Node.selectSingleNode("ValueId").text
					else
					ValueId = ""
				end if
				if NOT Node.selectSingleNode("Value") is nothing then
					Value = Node.selectSingleNode("Value").text
					else
					Value = ""
				end if
				
				
				if cint(VariableId) < 142 then 
					response.write "<hr>"
					response.write VariableId &"<br>"
					response.write Variable &"<br>"
					response.write ValueId &"<br>"
					response.write Value &"<br>"
					response.write "<hr>"
				end if
				'response.write VariableId &"<br>"
				'response.write Variable &"<br>"
				'response.write ValueId &"<br>"
				'response.write Value &"<br>"

        Next
		
		
       
      
       set xmlhttp = nothing
%>
 

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jason JonesGIS/APP DevAuthor Commented:
Thanks guys,  That exactly what I was looking for Scott!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.