Windows Scripting
  Home arrow Windows Scripting arrow Page 3 - 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 - Building the document tree


    (Page 3 of 4 )

    To continue building the document tree, we will continue adding elements and attaching them to their parent elements.

    Set objTrack = objParser.createElement("track")

    objTrackList.appendChild objTrack

    This bit of code creates a “track” element and adds it as a child of the trackList element. Now we can build references to our audio tracks in our playlist. Each track element represents one track and has several child elements that provide properties about that track.

    Set objLocation = objParser.createElement("location")

    objLocation.Text = "file:///URI/to/mp3/file.mp3"

    objTrack.appendChild objLocation

    The first element we will create is the location element. The location element is used to provide a URI to the location of the audio track. This is added in the same way as every other element to this point. Notice, however, that I’ve added an extra step here. The Text property is used to set the value of element. In other words, it provides the text that is found between the opening and closing location tags.

    Set objTitle = objParser.createElement("title")

    objTitle.Text = "Song Title"

    objTrack.appendChild objTitle

    Next, we will add a title element. This is used to provide the title of the audio track. This code is identical to the code we used to create the location element in the last step. Notice that both of them are using the appendChild method of the track element. This makes them both children of that element and, in turn, siblings to each other.

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

       <trackList>

           <track>

              <location>file:///URI/to/file.mp3</location>

              <title>Song Title</title>

           </track>

       </trackList>

    </playlist>

    A look at our file now would reveal something like the example above. Notice how the location and title elements appear at the same level as children of the track element.

    Set objCreator = objParser.createElement("creator")

    objCreator.Text = "Artist or Band"

    objTrack.appendChild objCreator

     

    Set objInfo = objParser.createElement("info")

    objInfo.Text = "http://URI/to/resource/info.html"

    objTrack.appendChild objInfo

     

    Set objAlbum = objParser.createElement("album")

    objAlbum.Text = "Album Title"

    objTrack.appendChild objAlbum

     

    Set objTrackNum = objParser.createElement("trackNum")

    objTrackNum.Text = "0"

    objTrack.appendChild objTrackNum

    We continue adding each of the other property elements for the audio track:  creator provides an artist or band name associated with the track, info provides a URL to more information about the track (such as where it can be purchased), album provides the track’s album title, and trackNum is used to indicate the track number on the album.

    You can now repeat the code for creating the track element and each of its children for every track that you wish to add to your playlist.

    Set objIntro = objParser.createProcessingInstruction( _

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

    objParser.insertBefore objIntro, objParser.childNodes(0)

    Finally, we need to add the very first line that defines this as an XML file. This is known as a processing instruction. The XML Document object provides the createProcessingInstruction method for this. It accepts two parameters. The first parameter indicates the type of processing instruction to be used and the second provides any additional parameters.

    <?xml version="1.0" encoding="UTF-8"?>

    Look at the line that I’m using to add this instruction. The childNodes property returns a collection of childNodes for an object, in this case, the document itself.  By specifying item 0 I am referring to the first element in that collection, or its first child element. Then I’m using the XML Document object’s insertBefore method. It accepts two parameters and inserts the object in the first parameter before the object in the second parameter. So this line inserts objIntro before the first element in our XML document, effectively making sure it always appears as the first line of the document.

    objParser.Save "C:Playlist.xml"

    At this point, the XML document resides only in memory. You can write it to a file using the XML Document object’s Save method.

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