Note that there are some explanatory texts on larger screens.

plurals
  1. POASP Classic - XML Dom
    text
    copied!<p>I've got the unpleasurable task of working on a Classic ASP site (VBSCRIPT) and need to parse out the following information in a loop.</p> <pre><code>&lt;xml&gt; &lt;product ref="xxx"&gt; &lt;xxx/&gt; &lt;xxx/&gt; &lt;xxx/&gt; &lt;images&gt; &lt;image ref="JCCCCCC" /&gt; &lt;image ref="JCCCCCD" /&gt; &lt;/images&gt; &lt;/product&gt; &lt;product ref="xxx"&gt; &lt;xxx/&gt; &lt;xxx/&gt; &lt;xxx/&gt; &lt;images&gt; &lt;image ref="JCCCCCC" /&gt; &lt;image ref="JCCCCCD" /&gt; &lt;/images&gt; &lt;/product&gt; &lt;/xml&gt; </code></pre> <p>I'm trying to grab the product refs and then the images (4th main node down)</p> <p>I've been faffing with this for a while now and am suffering brain block after not using ASP for over 2 years.</p> <pre><code>&lt;% 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("\") &amp; "\xmlupdate\product.xml") If objXML.parseError.errorCode &lt;&gt; 0 Then 'handle the error End If Set objLst = objXML.getElementsByTagName("Product") SizeofObject = objLst.length-1 response.Write(SizeofObject&amp;"&lt;br&gt;&lt;br&gt;") For i = 0 To (SizeofObject-1) Set objHnd = objLst.item(i) Response.Write(objHdl.childNodes(0).text) Next %&gt; </code></pre> <p>Any help would be great before I lose my mind to ASP</p> <p>--- Additional ---</p> <p>Using this provides a full output as I'd hope its the node attributes I cant seem to grab.</p> <pre><code>&lt;% Set objLst = objXML.getElementsByTagName("Product") SizeofObject = objLst.length-1 response.Write(SizeofObject&amp;"&lt;br&gt;&lt;br&gt;") For each elem in objLst set childNodes = elem.childNodes for each node in childNodes Response.Write node.nodeName &amp; " = " &amp; node.text &amp; "&lt;br /&gt;" &amp; vbCrLf next Response.Write "&lt;hr&gt;" &amp; vbCrLf Next %&gt; </code></pre> <hr> <h2>Final Code To Dump the XML (Cerebrus Below)</h2> <pre><code>&lt;% Set objLst = objXML.getElementsByTagName("Product") SizeofObject = objLst.length-1 response.Write(SizeofObject&amp;"&lt;br&gt;&lt;br&gt;") For each elem in objLst set childNodes = elem.childNodes for each node in childNodes Response.Write node.nodeName &amp; " = " &amp; node.text &amp; "&lt;br /&gt;" &amp; vbCrLf If lcase(node.nodeName)="images" then Response.Write("&lt;B&gt;Images Hit&lt;/B&gt;&lt;/br&gt;") set xattchildnodes = node.childNodes For Each attchildnodes in xattchildnodes For Each att in attchildnodes.Attributes Response.Write att.Name &amp; " = " &amp; att.text &amp; "&lt;br /&gt;" &amp; vbCrLf Next Next End If next Response.Write "&lt;hr&gt;" &amp; vbCrLf Next %&gt; </code></pre> <hr> <h2>Working XPATH Version (modified from Pete Duncanson Below)</h2> <pre><code>&lt;% Set objXML = Server.CreateObject("Microsoft.XMLDOM") objXML.Load (Server.MapPath("\") &amp; "\Product.xml") 'etc' Dim nodes set nodes = objXML.selectNodes("//xml/Product") Dim images For each node in nodes Response.Write("&lt;ul&gt;") Response.Write("&lt;li&gt;Ref: " &amp; node.getAttribute("ref") &amp; "&lt;/li&gt;") Set images = node.selectNodes("Images/Image") For each image in images Response.Write( "&lt;li&gt;Image:"&amp; image.getAttribute("ref") &amp;"&lt;/li&gt;" ) Next Response.Write( "&lt;/ul&gt;" ) Next %&gt; </code></pre> <hr> <p>Anthony Jones points out that its better to be specific so you may want to change</p> <pre><code>Set objXML = Server.CreateObject("Microsoft.XMLDOM") </code></pre> <p>to</p> <pre><code>Set objXML = Server.CreateObject("MSXML2.DOMDocument.3.0") </code></pre> <p>Which still works with the final code.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload