ASP Classic - XML ​​Dom

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.

+4
source share
3 answers

Yes, working in classic ASP sometimes takes me back to the Stone Age ... I feel your pain!

IIRC, in your second code snippet you just need to add:

 for each node in childNodes Response.Write node.nodeName & " = " & node.text & "<br />" & vbCrLf '***Add the following: For Each att in node.Attributes Response.Write att.Name & " = " & att.text & "<br />" & vbCrLf Next next 
+4
source

Switch to using xpath and it will be much easier.

 Dim nodes nodes = objXML.selectNodes( "//products" ) Dim images For each node in nodes Response.Write( "<ul>" ) Response.Write( "<li>Ref: " + node.selectNodes( "@ref" ).Text + "</li>" ) images = node.selectNodes( "images/image" ) For each image in images Response.Write( "<li>Image: " + image.selectNodes( "@ref" ).Text + "</li>" ) Next Response.Write( "</ul>" ) Next 

I am a JScript ASP encoder, for example, you did not do VBScript for age, so the above “may” need a little Polish (I had to cut all “;” at the end of all lines, this is the habit of adding them), but should point you in the right direction, at least.

Hope this helps.

+2
source

Try the following command to get the attribute value for the node image:

 node.Attributes.getNamedItem("ref").Text 
+1
source

All Articles