Retrieve XML data based on node id attribute

I need to find out how to access the xml data for only a certain store id.  I've attached a sample xml code and would like to know how I can retrieve just the data found for store with id "C1".  I want to store the childnode values into their respective variables e.g. <ccompanyname>Company1</ccompanyname> would be stored in a ccompany variable.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<store_variables xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<store id="C1">
		<ccompanyname>Company1</ccompanyname>
		<caddress>Address1A</caddress>
		<caddress2>Address2A</caddress2>
		<ccity>City1</ccity>
		<cstate>CA</cstate>
		<czip>90045</czip>
		<cemail>email1</cemail>
		<cphonenumber>phone1</cphonenumber>
		<cfaxnumber>fax1</cfaxnumber>
	</store>
	<store id="C2">
		<ccompanyname>Company2</ccompanyname>
		<caddress>Address1B</caddress>
		<caddress2>Address2B</caddress2>
		<ccity>City2</ccity>
		<cstate>CA</cstate>
		<czip>90045</czip>
		<cemail>email2</cemail>
		<cphonenumber>phone2</cphonenumber>
		<cfaxnumber>fax2</cfaxnumber>
	</store>
	<store id="C3">
		<ccompanyname>Company3</ccompanyname>
		<caddress>Address3A</caddress>
		<caddress2>Address3B</caddress2>
		<ccity>City3</ccity>
		<cstate>CA</cstate>
		<czip>90045</czip>
		<cemail>email3</cemail>
		<cphonenumber>phone3</cphonenumber>
		<cfaxnumber>fax3</cfaxnumber>
	</store>
	<store id="C4">
		<ccompanyname>Company4</ccompanyname>
		<caddress>Address4A</caddress>
		<caddress2>Address4B</caddress2>
		<ccity>City4</ccity>
		<cstate>CA</cstate>
		<czip>90045</czip>
		<cemail>email4</cemail>
		<cphonenumber>phone4</cphonenumber>
		<cfaxnumber>fax4</cfaxnumber>
	</store>
</store_variables>

Open in new window

LVL 3
UndisputedAsked:
Who is Participating?
 
hieloConnect With a Mentor Commented:
assuming your xml is from a file
Dim xml

xml="file.xml"
Set xmlDoc=Server.CreateObject("MSXML2.DOMDocument")
xmlDoc.load xml


xmlDoc.setProperty "SelectionLanguage", "XPath"

'this select the specific node you are after
Set node = xmlDoc.documentElement.selectSingleNode("//store_variables/store[@id='C1']")

'store has several nodes. The following iterates through these
For Each child in node.childnodes
	Response.write( "<br>"+child.text )
Next
Set node = Nothing
Set xmlDoc = Nothing

Open in new window

0
 
hieloConnect With a Mentor Commented:
If you already have the xml as a string in some variable named "xml", then use:
Set xmlDoc =Server.CreateObject("MSXML2.DOMDocument")
xmlDoc.loadXML xml


xmlDoc.setProperty "SelectionLanguage", "XPath"
Set node = xmlDoc.documentElement.selectSingleNode("//store_variables/store[@id='C1']")
For Each child in node.childnodes
	document.write( "<br>"+child.text )
Next
Set node = Nothing
Set xmlDoc = Nothing

Open in new window

0
 
UndisputedAuthor Commented:
Thanks Hielo!  This is exactly what I needed.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.