Results 1 to 2 of 2

Thread: Looping through XML

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2011
    Posts
    1

    Looping through XML

    I have the following XML file:

    <DirSpace>

    <Servers>
    <SvrName>SERVER01</SvrName>
    <dirName>"C:\dir1\"</dirName>
    <PathFileName>"C:\test1"</PathFileName>
    </Servers>


    <Servers>
    <SvrName>SERVER02</SvrName>
    <dirName>"C:\dir2\"</dirName>
    <PathFileName>"C:\test2"</PathFileName>
    </Servers>

    <Servers>
    <SvrName>SERVER03</SvrName>
    <dirName>"C:\dir3\"</dirName>
    <PathFileName>"C:\test3"</PathFileName>
    </Servers>



    </DirSpace>


    I am using the following code:

    Set objXMLDoc = CreateObject("Microsoft.XMLDOM")
    objXMLDoc.async = False

    objXMLDoc.load("test.xml")
    Dim root, i
    Set strNode = objXMLDoc.SelectNodes("//Servers")
    Set root = objXMLDoc.documentElement
    msgbox "No. of child nodes: "& root.childNodes.length
    Set objChildNodes = objXMLDoc.documentElement.childNodes


    For i = 0 To (root.childNodes.length)-1
    msgbox(root.childNodes.item(i).text)

    Set NodeList = Root.getElementsByTagName("SvrName")



    Next

    WHAT I really need to do is be able to loop through and do something like the following for a report :

    Server1 is using 32 GB in "C:\dir1\" and the path is "C:\test1\" has 3 files in it
    Server2 is using 32 GB in "C:\dir2\" and the path is "C:\test2\" has 3 files in it
    Server3 is using 32 GB in "C:\dir3\" and the path is "C:\test3\" has 3 files in it

    SO I need it to go to the first <SERVER></SERVER> and do something with that info and then go to the next set and so on and so forth. I need to be able to use variable names for like <SVRNAME></SVRNAME> so that I can have other things done based on that info.

    If you have any questions feel free to email me.

    Steve

  2. #2
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: Looping through XML

    From the description I can't tell what you are really trying to do but see if this gets you started.
    Code:
    Option Explicit
    
    Dim objXMLDoc, objNodeList, objNode
    Dim SvrName, dirName, PathFileName
    Dim strResults(), i
    
    	Set objXMLDoc = CreateObject("Microsoft.XMLDOM") 
    	objXMLDoc.async = False 
    
    	objXMLDoc.load("test.xml")
    	i = 0
    	
    	Set objNodeList = objXMLDoc.SelectNodes("//Servers")
    	
    	For Each objNode In objNodeList
    		SvrName = objNode.SelectSingleNode("SvrName").Text
    		dirName = objNode.SelectSingleNode("dirName").Text
    		PathFileName = objNode.SelectSingleNode("PathFileName").Text
    		Redim Preserve strResults(i)
    		strResults(i) = SvrName & " is using 32 GB in " & dirName & " and the path is " & PathFileName & " has 3 files in it"
    		i = i + 1
    	Next
    	
    	MsgBox Join(strResults, vbCrLf)
    	
    	Set objNode = Nothing
    	Set objNodeList = Nothing
    	Set objXMLDoc = Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width