Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Other Programming > VBScript
|
VBScript For questions and discussions related to VBScript.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VBScript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old February 27th, 2009, 05:57 AM
Registered User
 
Join Date: Feb 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Parsing xml using vbscript

Hi,

I need to parse a particular node in an xml file to get all the nodes and childnodes of each node under a particular node.
Can anyone help me with the code?
I found a function on devguru.com and i have modified it a bit. But gives error 'Object not a collection' code: 800A01C3

Code:
Sub Drill(n) 
  msgbox(n.nodeName & "<br>") 
  cc = 0 
  while n.hasChildNodes 
    cc = cc + 1 
    set chd = n.childnodes 
    for each cld1 in chd 
      msgbox(cld1.nodeName) 
      for each att in cld1.attributes 
        msgbox(cld1.nodeName & " " & att.name & " " & att.value) 
      next 
      If cld1.nodeName = "AName" Then 
        msgbox("AName "& cld1.Text) 
      End If 
      Drill(cld1) 
    next 
  wend 
End Sub

Last edited by saisudrik; February 27th, 2009 at 06:02 AM..
Reply With Quote
  #2 (permalink)  
Old February 27th, 2009, 07:40 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Doesn't make sense.

You are in an INFINITE LOOP there.
Code:
while n.hasChildNodes
You never break out of that loop and you never change the value of n inside the loop.

Also, since you are using recursion you *MUST* DIM each of the variables that are local to each occurrence of the recursion INSIDE the SUB.

That would mean
Code:
Sub Drill(n) 
    Dim chd, cld1, att
    ...
Finally, you are not checking to be sure that some of those values *exist* before you use them! For example, you do
Code:
for each att in cld1.attributes
But you *WILL* get an error if cld1 doesn't HAVE any attributes!
Reply With Quote
  #3 (permalink)  
Old February 27th, 2009, 08:03 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

See if this works:
Code:
Sub ShowOneNode( theNode ) 
    Dim atts, att, nodeinfo
    nodeinfo = theNode.NodeName

    Set atts = theNode.Attributes
    If Not ( atts Is Nothing ) Then
        For Each att In atts
            nodeinfo = nodeinfo & " [" & att.Name & "=" & att.Value & "]"
        Next
    End If
    MsgBox( "Node: " & nodeinfo )
    
    Dim kids, kid
    Set kids = theNode.ChildNodes
    If Not ( kids Is Nothing ) Then
        For Each kid In kids
            ShowOneNode( kid )
        Next
    End If
End Sub
All off the top of my head and untested. If you hand me a sample XML file, though, I'd be willing to test it out.
Reply With Quote





Similar Threads
Thread Thread Starter Forum Replies Last Post
parsing xml kri_hegde XML 5 July 24th, 2007 11:37 AM
XML parsing denzil_cactus Perl 0 June 11th, 2007 02:34 AM
XML Parsing SCADA_Monkey XML 2 March 11th, 2007 03:20 PM
XML Parsing tgopal Javascript 2 July 27th, 2004 08:54 AM
XML Parsing tgopal .NET Web Services 1 June 15th, 2004 03:25 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.