Saturday, January 30, 2010

XMLUtil - Name of root, count of root's children, count & names of children of any child element of root.

XMLUtil-4: How to get the NAME of the root element, get the COUNT of child elements of root, get COUNT & NAMES of children of any child element of root. Below code is showing COUNT & NAMES of children of 3rd child element of root. You can always change the code to suit your requirements.

XML file used:


Set doc = xmlutil.CreateXML
doc.LoadFile "d:\te.xml"
Set root = doc.GetRootElement
msgbox "Name of root element is: " & root.ElementName

Set children = root.ChildElements
msgbox "Root element has " & children.Count & " child elements."
q=cint (inputbox("Enter the no. of child element whose children you want to see "))
msgbox "No. of children of 3nd child of root are: " & children.Item(q).ChildElements.Count

For i= 1 to children.Item(q).ChildElements.Count
msgbox children.Item(q).ChildElements.Item(i).Value
Next

Result of running the above code:

 

  

  

  

 

  

And so on for ccc-3, ddd-3, eee-3.

Also See:
How to find total number of child elements.
How to view ALL elements in a message box, one at a time.
How to view the VALUE of elements in a message box, one at a time.
How to create a new XML file.
How to move/transfer text from XML file to QTP DataTable.
How to compare two XML files.