We help IT Professionals succeed at work.

Parsing XML with classic ASP

Kristen Jones
on
255 Views
Last Modified: 2018-11-01
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

Scott FellDeveloper
CERTIFIED EXPERT
Fellow
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
CERTIFIED EXPERT

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

Kristen 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
CERTIFIED EXPERT
Fellow
Most Valuable Expert 2013
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Kristen JonesGIS/APP Dev

Author

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

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.