Modifying XML Files in WSH
(Page 1 of 4 )
In my last two articles, I showed you how to create and read XML files in WSH. Today, I’m going to bring this series to a close by showing you how to modify XML files. This includes modifying the text data in the file as well as modifying the XML structure.
To begin, you'll need an XML file to work with. If you don't have the example file from the last two articles, you can grab it here.
Perhaps the most common reason for modifying an XML file is to append data. This is comparable to adding a new record to a database. Let's take our example XSPF playlist and add another set of track information as follows:
location: http://www.nilpo.com/pub/tracks/Crossfade%20-%20Cold.mp3
title: Dead Skin
creator: Crossfade
info: http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewAlbum?i=187470715%38id=187470617%38s=143441
album: Crossfade
trackNum: 2
The script first needs to open the XML file and load its document tree.
Set objXmlDoc = CreateObject("Microsoft.XMLDOM")
objXmlDoc.async = False
objXmlDoc.load("C:Playlist.xml")
This portion of the script returns an XML Document object by creating an instance of Microsoft's XML Parser. Next, the async property is set to false to help ensure that the script does not continue to execute until the file is fully loaded. Finally, the load method is used to load the XML document into the parser.
Set objRoot = objXmlDoc.documentElement
The XML Document object exposes a documentElement method which has the net effect of returning the first, or root, element of the document. By creating a reference to the root element, you can traverse the entire document tree from the top down. We don't have a need for this in today's example, but it's important to keep this in mind.
Next: Adding new elements >>
More Windows Scripting Articles
More By Nilpo