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.
Next: Extending the concept >>
More Windows Scripting Articles
More By Nilpo