473,407 Members | 2,326 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Iterating through the Child nodes of a Child Node

Hello;

I'm trying to iterate through the sub nodes of a child node, but I'm
getting a compile error on the nested For...Next expression saying it is
invalid. Using the below code, the HasChild() statement is "true" when
the 3rd item is reached so I know there are child elements below it. A
debug watch on the "SET oNodeList2 statement" shows that there is a list
present at run-time. What is wrong with the code on the FOR..NEXT
statement?

Thanks,
Gregg


Expand|Select|Wrap|Line Numbers
  1. set oNodeList = root.childnodes
  2. i = 0
  3. i2 = 0
  4. For Each Item In oNodeList
  5.      i = i + 1
  6.      if i < oNodeList.length then
  7.         msgbox "Listing the EXISTING nodes"
  8.         MsgBox oNodeList.item(i).nodename & " " &
  9. oNodeList.item(i).nodevalue & " " & oNodeList.item(i).nodetype
  10.         If oNodeList.item(i).hasChildNodes() then
  11.            MsgBox oNodeList.item(i).nodename & "has child nodes"
  12.            'set oNodeList2 =
  13. root.getelementsbytagname(oNodeList.item(i).nodename)
  14.            set oNodeList2 = oNodeList.item(i).childnodes
  15.            i2 = 0
  16.            For Each Item In oNodeList2
  17.                i2 = i2 + 1
  18.                MsgBox oNodeList2.item(i).nodename & " " &
  19. oNodeList2.item(i).nodevalue & " " & oNodeList2.item(i).nodetype
  20.                If oNodeList2.item(i).hasChildNodes() then
  21.                   MsgBox oNodeList2.item(i).nodename & "has child nodes"
  22.                end if
  23.            Next
  24.         end if        
  25.     end if
  26. Next
  27.  
  28.  
Jun 7 '07 #1
2 21770
dorinbogdan
839 Expert 512MB
Try something like this:

Expand|Select|Wrap|Line Numbers
  1. For Each node In oNodeList
  2.  
  3.         msgbox "Listing the EXISTING nodes"
  4.  
  5.         MsgBox node.nodename & " " & node.nodevalue & " " & node.nodetype
  6.  
  7.         If node.hasChildNodes() then
  8.  
  9.            MsgBox node.nodename & "has child nodes"
  10.  
  11.            set oNodeList2 = node.childnodes
  12.  
  13.            For Each node2 In oNodeList2
  14.  
  15.                MsgBox node2.nodename & " " & node2.nodevalue & " " & node2.nodetype
  16.  
  17.                If node2.hasChildNodes() then
  18.  
  19.                   MsgBox node2.nodename & "has child nodes"
  20.  
  21.                end if
  22.  
  23.            Next
  24.  
  25.         end if       
  26.  
  27.     end if
  28.  
  29. Next
Jun 8 '07 #2
Thank you. I now realize that ITEM in the For...Next isn't a keyword- I thought it was. Anyway, I've modified my approach so that I can display the full sequence of nodes in the file, but I'm not getting results I expect.

As you can see in the code, I built on your idea and am now using a repetitive function call.

Below is XML schema which I'm trying to iterate through node by
node. The script I'm using is also shown below. The resulting output,
also shown below, shows that I'm not reaching the element "tblTestCase"
nor the attribute names "TeamID" and "ProdID". How should I change my
script to make it follow the nodes all the way through?

Thanks,
Gregg

Please note that I've modified the XML for brevity and also so that it
isn't read as xml code by the post.

Expand|Select|Wrap|Line Numbers
  1. xsd:schema
  2. targetNamespace="http://schemas.microsoft.com/office/infopath/2003/ado/d
  3. ataFields" elementFormDefault="unqualified"
  4. attributeFormDefault="unqualified"
  5. xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  6. xmlns:d="http://schemas.microsoft.com/office/infopath/2003/ado/dataField
  7. s"
  8.     xsd:element name="tblTestCase"
  9.         xsd:complexType
  10.             xsd:attribute name="TeamID" use="optional"
  11.                 xsd:simpleType
  12.                     xsd:restriction base="xsd:string"
  13.                         xsd:maxLength value="10"/
  14.                     /xsd:restriction
  15.                 /xsd:simpleType
  16.             /xsd:attribute
  17.                         xsd:attribute name="ProdID" use="optional"
  18.                 xsd:simpleType
  19.                     xsd:restriction base="xsd:string"
  20.                         xsd:maxLength value="10"/
  21.                     /xsd:restriction
  22.                 /xsd:simpleType
  23.             /xsd:attribute
  24.  


OUTPUT FILE:
Expand|Select|Wrap|Line Numbers
  1. NodeName   NodeType
  2. ++++++++  ++++++++++
  3. mso-application progid="InfoPath.Document" 7
  4. dfs:myFields 1
  5. dfs:queryFields 1
  6. dfs:queryFields 1
  7. #text      3
  8. dfs:dataFields 1
  9. #text      3
  10. my:FileAttachment 1
  11. #text 3
  12. my:PassFail 1
  13. #text 3
  14.  
Expand|Select|Wrap|Line Numbers
  1. set oNodeList = root.childnodes
  2. MoreNodes = True
  3. Do While MoreNodes
  4.   i = 0
  5.   Display_NodeList oNodeList, i, MoreNodes
  6.  
  7. Loop
  8.  
  9. Sub Display_NodeList(varNodeList, varIndex, boolMoreNodes)
  10. boolMoreNodes = False
  11. For Each Item In varNodeList
  12.      varIndex = varIndex + 1
  13.      if varIndex < varNodeList.length then
  14.         strTextLine=varNodeList.item(varIndex).nodename & " " &
  15. varNodeList.item(varIndex).nodevalue & " " &
  16. varNodeList.item(varIndex).nodetype
  17.         write_lines strTextLine
  18.         'msgbox "Listing the EXISTING nodes"
  19.         'MsgBox varNodeList.item(varIndex).nodename & " " &
  20. varNodeList.item(varIndex).nodevalue & " " &
  21. varNodeList.item(varIndex).nodetype
  22.         If varNodeList.item(varIndex).hasChildNodes() then
  23.            boolMoreNodes = True
  24.            MsgBox varNodeList.item(varIndex).nodename & "has child
  25. nodes"
  26.            IF varNodeList.length = 9 then
  27.               do while i3 < 9
  28.                  i3 = i3 + 1
  29.                  strTextLine=varNodeList.item(i3).nodename & " " &
  30. varNodeList.item(i3).nodevalue & " " & varNodeList.item(i3).nodetype
  31.                  write_lines strTextLine
  32.               loop
  33.               'msgbox varNodeList.item(1).nodename
  34.               'msgbox varNodeList.item(2).nodename
  35.               'msgbox varNodeList.item(3).nodename
  36.               'msgbox varNodeList.item(4).nodename
  37.               'msgbox varNodeList.item(5).nodename
  38.               'msgbox varNodeList.item(6).nodename
  39.               'msgbox varNodeList.item(7).nodename
  40.               'msgbox varNodeList.item(8).nodename
  41.               'msgbox varNodeList.item(9).nodename
  42.            end if
  43.            'set oNodeList2 =
  44. root.getelementsbytagname(oNodeList.item(i).nodename)
  45.            set varNodeList = varNodeList.item(varIndex).childnodes
  46.            varIndex=0
  47.            'set oNodeList2 =
  48. oNodeList.selectnodes(oNodeList.item(i).nodename)
  49.            'set oNodeList2 =
  50. root.selectnodes(oNodeList.item(i).nodename)
  51.         end if
  52.         if varNodeList.item(varIndex).nodeType = 3 then
  53.            MsgBox "TEXT NODE: " & varNodeList.item(varIndex).nodename &
  54. " " & varNodeList.item(varIndex).nodevalue & " " &
  55. varNodeList.item(varIndex).nodetype
  56.            MSGBOX varNodeList.item(varIndex).text
  57.         end if
  58.         if varNodeList.item(varIndex).nodeType = 4 then
  59.            MsgBox "ATTR NODE: " & varNodeList.item(varIndex).nodename &
  60. " " & varNodeList.item(varIndex).nodevalue & " " &
  61. varNodeList.item(varIndex).nodetype
  62.            MSGBOX varNodeList.item(varIndex).text
  63.         end if
  64.      end if
  65. Next
  66.  
  67. end sub
  68.  
Jun 9 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: kaeli | last post by:
Can anyone explain this to me? It's driving me insane. Save this and run it in IE or Opera and then in Mozilla or Netscape 6+. In IE/Opera, I get the expected 4 alerts. In Mozilla/Netscape, I...
12
by: Dino L. | last post by:
I am putting data from DataTable to treeView foreach( DataRow aRow in aTable.Rows) { TreeNode tnode = new TreeNode(aRow.ToString() + aRow.ToString() + " " + aRow.ToString());...
7
by: amruta | last post by:
the code below dows not let me get the parent child view... all the nodes are show in one line only... also i need them to be collasped ... Thanks ..
2
by: Jack | last post by:
Hello, I am trying use a TreeView with checkboxes. I would like to check more than one node and allow all child nodes of selected nodes to be checked or unchecked with the parent is checked. ...
2
by: jon|k | last post by:
hi all-- i need to do a transformation that removes duplicates (among other things). to accomplish that, i'm trying to use for-each-group, but it doesn't work. i need to select for duplicates by...
1
by: Daniel Rucareanu | last post by:
Hello, Does anybody knows how can you delete, in just one step, not using a loop, a subset of the child nodes of a given DOM parent node? The subset will be continous, so for example, if the...
1
by: Garudzo | last post by:
Hi I am trying to use the Windows treeview in a MS Access application. I want to iterate through child nodes of a particular node but i am failing to figure out how to do it. I have even tried the...
6
by: dav61000 | last post by:
I have a parent form that uses a Treeview Control and I would like to pass the node selected to a child form where the user will add update or delete the record selected using a dataset. Is there a...
0
by: divya1949 | last post by:
Create a windows c# application which will Read a xml file and populate nodes in the treeview. 1 On selection of treenode display the child nodes of that node in listview control 2. ...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.