Windows Scripting
  Home arrow Windows Scripting arrow Page 4 - Modifying XML Files in WSH
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
WINDOWS SCRIPTING

Modifying XML Files in WSH
By: Nilpo
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 4
    2008-12-08

    Table of Contents:
  • Modifying XML Files in WSH
  • Adding new elements
  • Placing new elements
  • Modifying and deleting existing records

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

     

    WINDOWS SCRIPTING ARTICLES

    - More Windows Scripting Workarounds from Nilpo
    - Overloading Methods and More in VBScript
    - Improving MFC for Windows Vista
    - Regular Expressions in VBScript
    - Working with Dates in WMI
    - Completing Calendars with VBScript Date Func...
    - Building Calendars with VBScript Date Functi...
    - Working With Dates and Times in VBScript
    - Designing WCF DataContract Classes Using the...
    - Understanding Dates and Times in VBScript
    - Working With Arrays in VBScript
    - Compressed Folders in WSH
    - Using .NET Interops in VBScript
    - Nilpo`s Scripting Secrets, Vol I
    - Database operations using Silverlight 2.0 WC...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    Stay green...Green IT