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.
Next: Building the document tree >>
More Windows Scripting Articles
More By Nilpo