Windows Scripting
  Home arrow Windows Scripting arrow Page 3 - Reading XML Files in WSH
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
WINDOWS SCRIPTING

Reading XML Files in WSH
By: Nilpo
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2008-12-01

    Table of Contents:
  • Reading XML Files in WSH
  • Getting started
  • More advanced queries
  • Further filtering results
  • Constructing advanced queries
  • What to do when it doesn’t work

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Reading XML Files in WSH - More advanced queries


    (Page 3 of 6 )

    Okay, admittedly I haven’t done anything too fancy yet, have I? I’ve shown you how to return a single specific value from an XML file. But chances are good that if you’re using this as a database, you’re going to need to create much more complex queries than that.

    More specifically, you’re probably wondering how you can filter results. For instance, you might want to query a specific field, or element. Possibly, you only want to return track information for tracks performed by a specific artist. This is all possible, and then some.

    Set colTitles = objXmlDoc.selectNodes("/playlist/trackList/track/title")

     

    For Each objTitle In colTitles

       Wscript.Echo objTitle.Text

    Next

    If you only want your collection to return information from a specific element, you can avoid using a wildcard character and specify a particular field instead.

    The Diary Of Jane

    Who You Are

    Executing this bit of code will result in something similar to the example above.  I’m just generating a listing of the text value for a specific field; in this case, a list of each of the track's titles.

    The query returns a collection containing every element in the document that matches the document path provided—specifically, the title element of every track. Note how every instance of the title element in the sample document matches the path in our query string.

    Now suppose you want to return more than one specific element?

    Set colProperties = objXmlDoc.selectNodes( _

       "/playlist/trackList/track/(title | creator)")

     

    For Each objProperty In colProperties

       WScript.Echo objProperty.Text

    Next

    This piece of code returns any element that matches either title or creator. The syntax looks a bit rough, but think of the pipe (|) character as an OR operator.  The OR clause is enclosed in parenthesis.

    Set colProperties = objXmlDoc.selectNodes( _

       "/playlist/trackList/track/(location | title | creator)")

     

    For Each objProperty In colProperties

       WScript.Echo objProperty.Text

    Next

    Returning additional fields is as easy as adding them inside the parenthesis. Just remember to keep them separated by pipe characters.

    More Windows Scripting Articles
    More By Nilpo


     

    WINDOWS SCRIPTING ARTICLES

    - More Windows Scripting Workarounds from Nilpo
    - Overloading Methods and More in VBScript
    - Improving MFC for Windows Vista
    - Regular Expressions in VBScript
    - Working with Dates in WMI
    - Completing Calendars with VBScript Date Func...
    - Building Calendars with VBScript Date Functi...
    - Working With Dates and Times in VBScript
    - Designing WCF DataContract Classes Using the...
    - Understanding Dates and Times in VBScript
    - Working With Arrays in VBScript
    - Compressed Folders in WSH
    - Using .NET Interops in VBScript
    - Nilpo`s Scripting Secrets, Vol I
    - Database operations using Silverlight 2.0 WC...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek