Modifying XML Files in WSH - Modifying and deleting existing records
(Page 4 of 4 )
The second most common reason for modifying an XML file is probably to change the data that is stored in it-much like updating a record in a database. There are two distinct approaches you can take when modifying data in an XML file. The first of these is to simply change the text value of an existing element. Take our newly added song for instance. If you look at the links, the song title should have been "Cold," not "Dead Skin." Let's go ahead and change that now.
Set objTrack = objXmlDoc.selectSingleNode( _
"/playlist/trackList/track [title = 'Dead Skin']/title")
objTrack.text = "Cold"
Here the selectSingleNode is used to return a reference to the title element of the track record whose title is currently set to "Dead Skin." That object's text property can then be used to set the title element to a new value. You can also update multiple records at one time.
Set colNodes = objXmlDoc.selectNodes( _
"/playlist/trackList/track [title = 'Dead Skin']/title")
For Each objNode In colNodes
objNode.Text = "Cold"
Next
In this case, the selectNodes method is used to return a collection of any and all elements matching the criteria. A For Each loop can then be used to modify each matching instance.
Another approach to manipulating the data in an XML file is to replace one element with another. With this method, you can effectively overwrite an element with an updated element. For example, what if we didn't want to add this last song as a new record, but rather wanted to replace an existing song with this one instead?
Set objTrackList = objXmlDoc.selectSingleNode("/playlist/trackList")
Set objTrack = objXmlDoc.createElement("track")
objTrackList.replaceChild objTrack, objTrackList.lastChild
This is accomplished using the replaceChild method. The first of its two parameters specifies the newly created object and the second specifies the object that it is to replace. Here, I am simply creating a new track element and replacing the last one in the list.
Finally, you may find that you wish to remove a record (or element) to effectively delete it from the database. Each node object exposes a removeChild method for this. Let's remove the new song that we've just added.
Set objTrack = objXmlDoc.selectSingleNode( _
"/playlist/trackList/track [title = 'Cold']")
objTrack.parentNode.removeChild objTrack
I've used the selectSingleNode method to return a reference to the track element for the song we've just added to the XML file. In order to remove this element, I must access it from its parent element. Notice how I've used the track object's parentNode method to return a reference to its parent element. Then I've immediately called the removeChild method from the parent and supplied the track object as its parameter. I know that it seems a bit counter-intuitive, but unfortunately, there is no way to remove an element directly.
Set colTracks = objXmlDoc.selectNodes("/playlist/trackList/track")
For Each objTrack In colTracks
objTrack.parentNode.removeChild objTrack
Next
As you probably imagined, you could use this technique for a collection of objects as well. The example above will return every track element and then remove them one by one.
That wraps up this series on working with XML files. Over the course of three articles you've learned how to use the Microsoft XML Parser to create, read, and modify XML files. You'll find the more you use this, the easier it becomes. It's not half as hard as it seems on the surface. 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. |