Windows Scripting
  Home arrow Windows Scripting arrow Page 4 - Creating an XML Document 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

Creating an XML Document in WSH
By: Nilpo
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2008-11-24

    Table of Contents:
  • Creating an XML Document in WSH
  • Microsoft’s XML Parser
  • Building the document tree
  • Extending the concept

  • 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


    Creating an XML Document in WSH - Extending the concept


    (Page 4 of 4 )

    At this point you’ve learned how to create an XML document from within WSH.  However, you still need to manually add each element. Chances are, you are looking for a way to automate the entire process.

    We’re going to build a script that will create an XSPF playlist for all of the MP3 files found in your My Music folder.

    Set objParser = CreateObject("Microsoft.XMLDOM")

     

    WScript.Echo "Building XSPF Playlist..."

     

    Set objPlaylist = objParser.createElement("playlist")

    objParser.appendChild objPlaylist

    objPlaylist.setAttribute "version", "1"

    objPlaylist.setAttribute "xmlns", "http://xspf.org/ns/0/"

     

    Set objTrackList = objParser.createElement("trackList")

    objPlaylist.appendChild objTrackList

    The script begins, as you might imagine, by creating the beginnings of an XML document. We’re simply connecting to the XML Document object and beginning to build the document tree, as you’ve already seen.

    <playlist version="1" xmlns="http://xspf.org/ns/0/">

       <trackList>

    At this point, our XML file is pretty basic. Now we’re ready to begin adding the track information for each of our MP3 files.

    Set objShell = CreateObject("Shell.Application")

    Set objFolder = objShell.NameSpace(&Hd&)

    Set colFiles = objFolder.Items

    To do so, I’ve used the Shell Application object’s NameSpace method to return a ShellFolder object for the My Music folder. The “&Hd&” is a shell folder constant that represents the My Music folder for the currently logged-on user. I’m then using the ShellFolder object’s Items property to return a collection of all the files in that folder.

    For Each objFile In colFiles

       strType = objFolder.GetDetailsOf(objFile, 2)

       If strType = "MP3 Format Sound" Then

     

           WScript.Echo "Adding", objFile.Name & ".mp3"

     

           Set objTrack = objParser.createElement("track")

           objTrackList.appendChild objTrack

             

           Set objLocation = objParser.createElement("location")

           objLocation.Text = objFile.Path

           objTrack.appendChild objLocation

             

           Set objTitle = objParser.createElement("title")

           objTitle.Text = objFolder.GetDetailsOf(objFile, 10)

           objTrack.appendChild objTitle

             

           Set objCreator = objParser.createElement("creator")

           objCreator.Text = objFolder.GetDetailsOf(objFile, 16)

           objTrack.appendChild objCreator

             

           Set objAlbum = objParser.createElement("album")

           objAlbum.Text = objFolder.GetDetailsOf(objFile, 17)

           objTrack.appendChild objAlbum

             

           Set objTrackNum = objParser.createElement("trackNum")

           objTrackNum.Text = objFolder.GetDetailsOf(objFile, 19)

           objTrack.appendChild objTrackNum

       End If

    Next

    Now it’s a simple matter of looping through each of the files in the colFiles collection. I’m making healthy use of the Shell Application object’s GetDetailsOf method. This method reads a file’s details, including ID3 tag information. I’m first using it to filter out only MP3 files. Then I’m using it to return the information to be used as the text part of the XML track elements.

    Note that this script is designed to run on Windows XP. For Vista, you will need to change the integer parameter being used. For more information, read my article titled Reading MP3 ID3 tags in WSH.

    Set objIntro = objParser.createProcessingInstruction( _

       "xml","version='1.0' encoding='UTF-8'")

    objParser.insertBefore objIntro,objParser.childNodes(0)

     

    objParser.Save "C:Playlist.xml"

     

    WScript.Echo "...Building Complete"

    Finally, the script is completed by adding the processing instruction and saving the file.

    That’s all there is to it. The Microsoft XML Parser makes it very easy and efficient to create XML files. It also provides several methods for reading and updating XML files as well, but I’ll save those for future articles. Until next time, keep coding!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

     

    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 1 Hosted by Hostway
    Stay green...Green IT