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. |