Parsing XML with classic ASP

Jason Jones
Jason Jones used Ask the Experts™
on
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

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Scott FellDeveloper & EE Moderator
Fellow 2018
Most Valuable Expert 2013

Commented:
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 Developer

Commented:
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 Dev

Author

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>
Developer & EE Moderator
Fellow 2018
Most Valuable Expert 2013
Commented:
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

Jason JonesGIS/APP Dev

Author

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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial