Posts tagged with 'xp'
Of all the blog post titles I'd like to write, "Parsing XML in ASP classic" is definitely not at the top of my list. But sometimes you just have to suck it up. So here we go...
Given a string that contains XML (maybe the result of an Ajax request or the contents of some config file), let's get some values out of it.
Here's some sample XML that I'll be using:
<USER_INFO ShoeSize="13" FirstName="Matthew" LastName="Groves" City="Funkytown, USA" /> |
Create a MSXML2.DOMDocument.6.0 object. Use its LoadXML method. You can then use the selectSingleNode and XPath to get values out. For instance, if I wanted the value for ShoeSize in the above XML, I could use an XPath of //@ShoeSize to get a node. Then use the text property of that node to get the value.
<% | |
dim myXml 'assume that this is populate with an XML string | |
dim oXml | |
Set oXml = Server.CreateObject("MSXML2.DOMDocument.6.0") 'don't forget to use Set | |
oXml.LoadXML(myXml) | |
dim node | |
Set node = oXml.selectSingleNode("//@ShoeSize") 'don't forget to use Set | |
response.write "Shoe Size: " & node.text & "<br />" 'outputs: "Shoe Size: 13<br />" | |
Set node = oXml.selectSingleNode("//@City") 'don't forget to use Set | |
response.write "City: " & node.text & "<br />" 'outputs: "City: Funkytown, USA<br />" | |
%> |
There ya go. If you aren't an XPath whiz, you can use this XPath tester to help you through it.