I have an unsatisfactory task to work on a classic ASP site (VBSCRIPT) and I need to parse the following information in a loop.
<xml> <product ref="xxx"> <xxx/> <xxx/> <xxx/> <images> <image ref="JCCCCCC" /> <image ref="JCCCCCD" /> </images> </product> <product ref="xxx"> <xxx/> <xxx/> <xxx/> <images> <image ref="JCCCCCC" /> <image ref="JCCCCCD" /> </images> </product> </xml>
I am trying to capture ref ref and then images (4th main node down)
I have come across this for some time now, and I am suffering from brain block after not using ASP for more than two years.
<% Set objXML = Server.CreateObject("Microsoft.XMLDOM") Set objLst = Server.CreateObject("Microsoft.XMLDOM") Set objHdl = Server.CreateObject("Microsoft.XMLDOM") objXML.async = False objXML.Load (Server.MapPath("\") & "\xmlupdate\product.xml") If objXML.parseError.errorCode <> 0 Then 'handle the error End If Set objLst = objXML.getElementsByTagName("Product") SizeofObject = objLst.length-1 response.Write(SizeofObject&"<br><br>") For i = 0 To (SizeofObject-1) Set objHnd = objLst.item(i) Response.Write(objHdl.childNodes(0).text) Next %>
Any help would be great before I pass out ASP
--- Optional ---
Using this, you will get the full output, since I hope that its node attributes, which I did not seem to capture.
<% Set objLst = objXML.getElementsByTagName("Product") SizeofObject = objLst.length-1 response.Write(SizeofObject&"<br><br>") For each elem in objLst set childNodes = elem.childNodes for each node in childNodes Response.Write node.nodeName & " = " & node.text & "<br />" & vbCrLf next Response.Write "<hr>" & vbCrLf Next %>
Final XML Reset Code (below Cerebrus)
<% Set objLst = objXML.getElementsByTagName("Product") SizeofObject = objLst.length-1 response.Write(SizeofObject&"<br><br>") For each elem in objLst set childNodes = elem.childNodes for each node in childNodes Response.Write node.nodeName & " = " & node.text & "<br />" & vbCrLf If lcase(node.nodeName)="images" then Response.Write("<B>Images Hit</B></br>") set xattchildnodes = node.childNodes For Each attchildnodes in xattchildnodes For Each att in attchildnodes.Attributes Response.Write att.Name & " = " & att.text & "<br />" & vbCrLf Next Next End If next Response.Write "<hr>" & vbCrLf Next %>
Working version of XPATH (modified below by Pete Duncanson)
<% Set objXML = Server.CreateObject("Microsoft.XMLDOM") objXML.Load (Server.MapPath("\") & "\Product.xml") 'etc' Dim nodes set nodes = objXML.selectNodes("//xml/Product") Dim images For each node in nodes Response.Write("<ul>") Response.Write("<li>Ref: " & node.getAttribute("ref") & "</li>") Set images = node.selectNodes("Images/Image") For each image in images Response.Write( "<li>Image:"& image.getAttribute("ref") &"</li>" ) Next Response.Write( "</ul>" ) Next %>
Anthony Jones points out that it's better to be specific, so you can change
Set objXML = Server.CreateObject("Microsoft.XMLDOM")
to
Set objXML = Server.CreateObject("MSXML2.DOMDocument.3.0")
which still works with the final code.