Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

xml/dom/vbscript confused about selectNodes()

Posted on 2004-09-12
2
5,316 Views
Last Modified: 2008-01-16
Hello,

I want to write a function that initially will print out values of my xml file. I only want to print a certain part of the document so I use a selectNodes() call with some xpath syntax that maybe incorrect. Here is my xml document structure

<profile>
 <discovery>
 </discovery>
<changes>
   <Network_Drives>
     <network_drive id_number="0" drive_letter="X:" old_mapping="\\server1\files2" new_mapping="\\server2\files2" />
     <network_drive id_number="1" drive_letter="Y:" old_mapping="\\server2\file\test" new_mapping="\\server1\file\test"/>
    </Network_Drives>
 </changes>
</profile>

so I am trying to print out the lines inbetween <Network_Drives> to get something like this:
 
 drive_id  0
 drive_letter x:
  etc....
The output could be on a single line or spread over multiple lines I just want to access each <network_drive>'s data and be able to store it into a variable eventually. First step would be printing out the data so I know its there.

I do not really even need the descriptions (drive_id, letter) all I want to do is spit out the data (o,x: etc..)

So I have this vbscript going right now

path = "C:\myfile.xml"
Set objXMLDoc = CreateObject("Microsoft.XMLDOM")
objXMLDoc.async = False
objXMLDoc.load(path)
'Set xmlDoc = CreateObject("Msxml2.DOMDocument.4.0")
'xmlDoc.loadXML(path)
WScript.Echo "xml document loaded"

Set NodeList = objXMLDoc.documentElement.selectNodes("//Changes/Network_Drives")
WScript.Echo "Length " & NodeList.length
For Each Node in NodeList
 WScript.Echo Node.Text
Next

My output right now is:

xml document loaded
Length 1

And nothing prints from my loop.....(which I dont understand)

Can anyone help me out with my vbscript code to accomplish my desired output?

Thanks,
Barry



0
Comment
Question by:barryk2
  • 2
2 Comments
 
LVL 19

Accepted Solution

by:
drichards earned 500 total points
ID: 12041235
You need to get the attributes values from the network_drive elements.  I changed the XPATH a bit (added 'network_drive') and added the code to get the attributes values.

    path = "C:\myfile.xml"
    Set objXMLDoc = CreateObject("Microsoft.XMLDOM")
    objXMLDoc.async = False
    objXMLDoc.load(path)
    WScript.Echo "xml document loaded"

    Set NodeList = objXMLDoc.documentElement.selectNodes("//changes/Network_Drives/network_drive")
    WScript.Echo "Length " & NodeList.length
    For Each Node in NodeList
      WScript.Echo "ID: " & Node.attributes.getNamedItem("id_number").value & " Drive: " & Node.attributes.getNamedItem("drive_letter").value
    Next


You could also use indexes rather than names:

      WScript.Echo "ID: " & Node.attributes.item(0).value & " Drive: " & Node.attributes.item(1).value
0
 
LVL 19

Expert Comment

by:drichards
ID: 12041243
Nothing printed in your original code because you were trying to print the text of the 'Network_Drives' node and there is no text.  'Network_Drives' contains only other elements which also do not have text.

Also, I had to change capitalization of "changes" in the XPATH.  I don't know if this was due to a typo.  I suspect so because your code would have done nothing if it was incorrect.  You may need to change it back to a capital to work on your XML file.  The sample XML you gace here had a lower case c.
0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Unattended XML settings 4 122
Formatting issues in XSL FO 3 44
XSLT 1.0 - Finding Substrings from the End of a String 3 32
How to implement an additional command 16 40
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question