Windows Scripting
  Home arrow Windows Scripting arrow Page 2 - 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 - Microsoft’s XML Parser


    (Page 2 of 4 )

    XML files use an interpreter known as a parser. Every modern browser has one, and Windows ships one natively as a part of Internet Explorer. The Microsoft XML Parser includes a scripting interface that allows us to easily read and create XML files.

    I know what you’re thinking. “You said that XML files were just text files. Why not just use the FileSystemObject?”

    The truth is that you could use the FileSystemObject. However, attempting to parse out elements and their attributes could prove to be a tedious task, considering there’s already a tool available to do the job.

    Set objParser = CreateObject("Microsoft.XMLDOM")

    With that being said, our script must first connect to the Microsoft XML Parser object. This line of code creates a new blank XML Document object. We can then load an existing XML file into that document or, as in our case, we can begin constructing a new one by building an element tree.

    Set objPlaylist = objParser.createElement("playlist")

    objParser.appendChild objPlaylist

     

    We’re going to begin by creating our root element “playlist.” The XML Document object provides the createElement method. Note that this only creates the element in memory. It doesn’t actually attach it to the document. To do that, we’ll need to use the appendChild method.

    The appendChild method will attach our new element to the document tree.  Since we are calling appendChild from the document object itself, our new element is attached as a child of the document object, making a new root level element within the document.

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

    If you look back at our example, you will see that the playlist element has two attributes associated with it. One is associated with the version number of the XSPF specification that we are using, and the other binds to the XSPF namespace.

    objPlaylist.setAttribute "version", "1"

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

    We can add these attributes to the playlist element using the setAttribute method provided by its object reference. The method accepts two parameters; the first is a text string that indicates the attribute name and the second is a text string indicating its value.

    Set objTrackList = objParser.createElement("trackList")

    objPlaylist.appendChild objTrackList

    We continue building our document by creating the next element. The trackList element is a child of the playlist element. So again, we will use the XML Document object’s createElement method to create an element reference. This time, however, we want this element to be a child of the playlist element so we will be calling the appendChild method from the playlist method rather than from the document object as before.

    This element should be named “trackList” exactly. The capitalized letter is part of the XSPF specification.

    At this point, our XML document should look something like this:

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

       <trackList>

    Note that if you open the document in a text editor at any point throughout this article, you may not see it as nicely formatted as I’m showing you here. Your document may appear as one long single line of text. This is not a problem. Your XML file will work just as well either way. I’m only formatting my examples for readability purposes.

    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 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek