2

Here is my XML doc:

<FileScan>
    <Sites>
        <Site>
            <Name>Joe</Name>
            <Dir>\\webserver\ftp\Joe</Dir>
            <Email>[email protected]</Email>
            <Subject>Hi Joe!</Subject>
        </Site>
        <Site>
            <Name>Ben</Name>
            <Dir>\\webserver\ftp\ben</Dir>
            <Email>[email protected], [email protected]</Email>
            <Subject>Hi Ben!</Subject>
        </Site>
        <Site>
            <Name>Ian</Name>
            <Dir>\\webserver\ftp\Ian</Dir>
            <Email>[email protected]</Email>
            <Subject>You are fired!</Subject>
        </Site>
        <Site>
            <Name>Mark</Name>
            <Dir>\\webserver\ftp\Mark</Dir>
            <Email>[email protected]</Email>
            <Subject>Hi Mark</Subject>
        </Site>
    </Sites>
</FileScan>

What I want to do is:

Open xmlDoc
Do while xmlDoc.eof <> true
   store <Name> to varNAME
   store <Dir> to varDIR
   store <Email> to varEMAIL
   store <Subject> to varSUBJECT
   Then run an already designed function
   Move to next XML <Site> tag
Loop

Currently this is what I have:

Set xmlDoc = CreateObject("Microsoft.XMLDOM")

xmlDoc.Async = "False"
xmlDoc.Load("FileScan.xml")
Set colNodes = xmlDoc.selectNodes ("//Site/ (Name|Dir)")
For Each objNode in colNodes
    WScript.Echo objNode.nodeName & ": " & objNode.text
Next

I am getting the Name and Dir in the popup, but I cant seem to store them into variables by element name. How can I refer to an element by its particular name and be sure it is the right element. Then do that over and over as the number of sites may change over time?

1
  • It's not quite clear to me what you mean by "store them into variables by element name". Where do you want to store the values? Do you need a list of entities storing the information from the Site nodes? Commented Nov 8, 2014 at 17:33

1 Answer 1

3

First, make sure you add a closing </FileScan> to your example.

This code will show you how to work it out. Be sure to debug with something like cscript /x your script.vbs to step through everything.

You'd probably want to check that the XML loaded (look for parseError), and that objSite has a the proper child element (eg Name, Dir), before using the text property.

Set xmlDoc = CreateObject("Microsoft.XMLDOM")

xmlDoc.Async = "False"
xmlDoc.Load("test.xml")
Set colNodes = xmlDoc.selectNodes ("//Site")
Dim strName
Dim strDir
WScript.Echo "Start"
For Each objSite in colNodes
    strName = objSite.selectSingleNode( "Name" ).text
    strDir = objSite.selectSingleNode( "Dir" ).text

    WScript.Echo "Test: " + strName + ":" + strDir
Next
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much. I was attempting to make this alot more difficult that what it actually needed to be.
Glad to help. I get paid in points, would you accept this as the answer? Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.