Tutorial: Samplescript - Parse and change XML-files

Technical support and scripting issues

Moderators: JRL, Dorian (MJT support)

Post Reply
durexlw
Newbie
Posts: 18
Joined: Mon Mar 17, 2008 1:19 pm

Tutorial: Samplescript - Parse and change XML-files

Post by durexlw » Wed Mar 19, 2008 1:31 pm

I was working on a project yesterday and it took me quite some time putting all the pieces together. I'd sure wish I could have found an example like I'm about to post here, so it felt like a good idea to post my results.

This is VBScript example-code to parse and change an XML-file:

Here's a part of the XML file
(A bit of a changed appearance, otherwise it wouldn't be displayed in this forum... just imagine the '-' to be ''):

Code: Select all

-NML MODE="PROTOCOL" VERSION="6"-

  -HEAD /-

  -PLAYLIST BASE_DIR="/" BASE_VOLUME="F:" LOCKED="0" TITLE="TDS2_2005-11-26_19h15m31"-
    -ENTRY ARTIST="" AUDIO_ID="AJcAEREzMzMjQiMzM0M0QyNCIzMzQzRVM1VEVGVkNEVmY0Q" DECK="0" DURATION="150" ID="901213" LOCK="0" START_DATE="2005/11/26" START_TIME="19:19:25" TITLE="Dave Berry - This Strabnge Effect"-
      -LOCATION DIR="/Music/" FILE="Dave Berry - This Strabnge Effect.mp3" PATH="F:\Music\" VOLUME="F:"/-
      -INFO BITRATE="128000" GENRE="slow 1 2 3" IMPORT_DATE="2004/7/28" LAST_PLAYED="2005/11/26" PLAYCOUNT="20" PLAYTIME="151" RANKING="0"/-
      -TEMPO BPM="102.231819" BPM_QUALITY="18"/-
      -LOUDNESS PEAK_DB=" 1.401897" PERCEIVED_DB=" 6.389466"/-
    -/ENTRY-
    -ENTRY ARTIST="Another Level" AUDIO_ID="ASAAIzMzIiI1ZlRVQyFEMzMyIjQzNDM0MiQ0M0NUMiIiNVVD" DECK="1" DURATION="288" ID="901215" LOCK="0" START_DATE="2005/11/26" START_TIME="19:24:08" TITLE="From The Heart"-
      -LOCATION DIR="/Music/" FILE="Another Level - From The Heart.mp3" PATH="F:\Music\" VOLUME="F:"/-
      -ALBUM TITLE="no title"/-
      -INFO BITRATE="128000" GENRE="knuffelrock" IMPORT_DATE="2004/7/28" LAST_PLAYED="2005/11/26" PLAYCOUNT="19" PLAYTIME="288" RANKING="0"/-
      -TEMPO BPM="80.980003" BPM_QUALITY="100"/-
      -LOUDNESS PEAK_DB=" 1.364597" PERCEIVED_DB=" 2.699466"/-
    -/ENTRY-
  -/PLAYLIST-

-/NML-
The VBScript Code:

Code: Select all

Function ReadXML(sCollectionPath, sBackupPath, sVolumeTarget)
   Dim objXML, x, y, z
   Dim iCounterEntry
   Dim sVersion
   Dim sVolumeSource
   Dim sPathSource
   Dim sPathTarget

   iCounterEntry=0
   set objXML = CreateObject("Microsoft.XMLDOM")
   objXML.preserveWhiteSpace = "true"
   objXML.async = "false"
   objXML.load(sCollectionPath) 'Load the XML-file

   sVersion = objXML.documentElement.getAttribute("VERSION") 								'objXML.documentElement = Rootnode... root.getAttribute is to read the attribute in the rootnode that's called 'VERSION'

   Select Case sVersion
      Case 6
         For Each x in objXML.documentElement.getElementsByTagName("PLAYLIST")				'X becomes a collection of all Root.childNodes that are called 'PLAYLIST'
            For Each y in x.getElementsByTagName("ENTRY")									'y becomes a collection of all Root.Playlist.childNodes that are called 'ENTRY'
      	       For Each z in y.getElementsByTagName("LOCATION")								'z becomes a collection of all Root.Playlist.Entry.childNodes that are called 'LOCATION'
      		      iCounterEntry = iCounterEntry + 1											'an entry is about to get evaluated, so increase the counter
      		      sVolumeSource = z.getAttribute("VOLUME")									'sVolumeSource = the attribute of 'Root.Playlist.Entry.Location' that's called 'VOLUME'
      		      sPathSource = z.getAttribute("PATH")										'sPathSource = the attribute of 'Root.Playlist.Entry.Location' that's called 'PATH'
      		      sPathTarget = Replace (sPathSource, sVolumeSource, sVolumeTarget, 1, 1)   'Stringreplace the volume in the path that was just derived from the Path-attribute
				  z.setAttribute "VOLUME", sVolumeTarget									'In the XML, change the Volume-Attribute-value to the new volume
				  z.setAttribute "PATH", sPathTarget										'In the XML, change the path-attribute-value to the new path
      		   Next
      	    Next
         Next
		 
		 objXML.save(sBackupPath)           'Save the changes in a new XML file

      Case Else 'Unknown Collection
	     MsgBox "Unknown collectionfile... Contact Admin and send him the collection file to implement it", , "Error"
   End Select

   ReadXML = iCounterEntry           'Return the number of lines that are changed
End Function


Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts