views:

1950

answers:

3

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.

<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'm trying to grab the product refs and then the images (4th main node down)

I've been faffing with this for a while now and am suffering brain block after not using ASP for over 2 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 lose my mind to ASP

--- Additional ---

Using this provides a full output as I'd hope its the node attributes I cant seem to grab.

<%
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 Code To Dump the XML (Cerebrus Below)

<%
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 XPATH Version (modified from Pete Duncanson Below)

<%
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 its better to be specific so you may want to change

Set objXML = Server.CreateObject("Microsoft.XMLDOM")

to

Set objXML = Server.CreateObject("MSXML2.DOMDocument.3.0")

Which still works with the final code.

+2  A: 

Switch to using xpath instead 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'm a JScript ASP coder, like you not done VBScript for an age so the above "might" need a bit of polish (I had to strip out all the ";" at the end of the all the lines, such is the habit of adding them) but should point you in the right direction at least.

Hope that helps.

Pete Duncanson
ditto; cant stand coding without proper closures, every syntax error turns out to be a semicolon that slipped in.
Chris M
Chris M
Using image.selectNodes("@ref").Text is a bug, the NodeList object returned by selectNodes does not have a Text property. Use node.getAttribute("ref") instead. Otherwise its reasonably well polished ;)
AnthonyWJones
Great entry, XPath is a preffered method, thanks.Modified working version added to the end of my question
Chris M
A: 

try the following command to get the attribute value specifically for the image node.

node.Attributes.getNamedItem("ref").Text

Vikram
+2  A: 

Yeah, having to work in classic ASP occasionally transports me back to the Stone age too... 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
Cerebrus
Few minor changes (as above) but just what the doctor ordered; many thanks :o)
Chris M
Most welcome. My pleasure. :-)
Cerebrus
Added to the end of the question. Thanks
Chris M