Windows Scripting
  Home arrow Windows Scripting arrow Page 3 - 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 - Placing new elements


    (Page 3 of 4 )

    The previous example demonstrates the simplest approach to adding new records by appending them as children to the existing document tree.  Sometimes, however, you may find that you don't want your newly created element to appear as the last element on the tree. For example, perhaps you wish for it to be the first element.

    Set objTrackList = objXmlDoc.selectSingleNode("/playlist/trackList")

    Set objFirstChild = objTrackList.firstChild

    In this example, I'm taking a slightly different approach. The selectSingleNode method returns a reference to the trackList element. That reference exposes a firstChild method that returns a reference to the object's first child object. In our case, this is the first track element.

    Set objTrack = objXmlDoc.createElement("track")

    objTrackList.insertBefore objTrack, objFirstChild

    Now, we can create a new track element and place it in the document as the first child. To do so, we'll need to use the insertBefore method of the trackList object.  It accepts two parameters: the first is an object reference to the element to be inserted into the document tree and the second is an object reference to the element before which it should be inserted. In this case, the insertBefore method is inserting the newly created track element as a sibling immediately before the first existing track element.

    There are several other methods available that can be used to place newly created elements into the document tree as well.

    The firstChild method has a counterpart lastChild method. The lastChild method will return a reference to the last child element for an object.

    Along with the firstChild and lastChild method, you can also find references to other elements using the previousSibling and nextSibling methods. Just as their names imply, these methods return references to the elements immediately preceding and following an object, respectively. These methods can often be used in conjunction to create the desired affect.

    Set objTrackList = objXmlDoc.selectSingleNode("/playlist/trackList")

    Set objFirstChild = objTrackList.firstChild

    Set objSecondChild = objFirstChild.nextSibling

     

    Set objTrack = objXmlDoc.createElement("track")

    objTrackList.insertBefore objTrack, objSecondChild

    This example will insert the newly created element as the second record. It first uses the firstChild method to return a reference to the first track element. Then, the nextSibling method is used to return a reference to the second track element. Finally, the insertBefore method is used to insert the newly created track element before the second existing one, thus making the new element the second element.

    More Windows Scripting Articles
    More By Nilpo


     

    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 5 Hosted by Hostway
    Stay green...Green IT