How to mass replace 'inner text' in specific keys in XML file? (2)
Download: 20111223.Profiling.XML.MassReplace as zip package
;==========================================================================================================================================
;
; How to mass replace 'inner text' in specific keys in XML file? (2)
;
; The 'Binary Buffer' Version.
;
; (c)Detlev Dalitz.20111218.
;==========================================================================================================================================

;-----------------------------------------------------
;   Ini file "Config.ini".
;
;   [IniFilePass]
;   C01_01=Pass
;   C01_02=Pass
;   C01_03=Pass
;   C01_04=Pass
;   D01_02=Pass
;   D01_03=Not Reporting
;   D01_04=Not Reporting
;   D01_05=Pass
;   E01_01=Pass
;   E01_02=Pass
;   E01_03=Confidential - Not Provided
;   E01_04=Pass
;   E01_05=Pass
;   E01_06=Pass
;   E01_07=Pass
;   E01_08=Not Reporting - Privileged Information
;   E01_09=Pass
;   E01_10=Pass
;-----------------------------------------------------

; Define working folder.
strFolderHome = DirScript ()
DirChange (strFolderHome)

; Define data file pathes.
strFileIni = strFolderHome : "Config.ini"
strFileXmlIn = strFolderHome : "Data.in.xml"
strFileXmlOut = strFolderHome : "Data.out.xml"

; For sure, remove existing output file.
If 0 != FileExist (strFileXmlOut) Then FileDelete (strFileXmlOut)


; Step 1. Calculate possibly growing file size.

; Allocate buffer for input file.
intFileSize = FileSize (strFileXmlIn)
hdlBB = BinaryAlloc (intFileSize)

; Read XML file into buffer.
intBytesRead = BinaryRead (hdlBB, strFileXmlIn)

; Open ini file and walk through the items.
intSizeDiff = 0
strIniSection = "IniFilePass"
strListItems = IniItemizePvt (strIniSection, strFileIni)
intItems = ItemCount (strListItems, @TAB)
For intI = 1 To intItems
   strKey = ItemExtract (intI, strListItems, @TAB)
   strValue = IniReadPvt (strIniSection, strKey, "Pass", strFileIni)
   If strValue == "Pass" Then Continue

   hdlBBTag = BinaryTagInit (hdlBB, "<" : strKey : ">", "</" : strKey : ">")
   While @TRUE
      hdlBBTag = BinaryTagFind (hdlBBTag)
      If hdlBBTag == "" Then Break
      strExtract = BinaryTagExtr (hdlBBTag, 0) ; Extract existing value.
      intSizeDiff = intSizeDiff + StrByteCount (strValue, -1) - StrByteCount (strExtract, -1) ; Measure change of size.
   EndWhile
Next

If intSizeDiff > 0
   hdlBB = BinaryFree (hdlBB) ; Close buffer.
   hdlBB = BinaryAlloc (intFileSize + intSizeDiff) ; Allocate new buffer.
   intBytesRead = BinaryRead (hdlBB, strFileXmlIn) ; Read XML file into buffer.
EndIf


; Step 2. Do the replacements.

; Open ini file and walk through the items.
strIniSection = "IniFilePass"
strListItems = IniItemizePvt (strIniSection, strFileIni)
intItems = ItemCount (strListItems, @TAB)
For intI = 1 To intItems
   strKey = ItemExtract (intI, strListItems, @TAB)
   strValue = IniReadPvt (strIniSection, strKey, "Pass", strFileIni)
   If strValue == "Pass" Then Continue

   hdlBBTag = BinaryTagInit (hdlBB, "<" : strKey : ">", "</" : strKey : ">")
   While @TRUE
      hdlBBTag = BinaryTagFind (hdlBBTag)
      If hdlBBTag == "" Then Break
      hdlBBTag = BinaryTagRepl (hdlBBTag, "<" : strKey : ">" : strValue : "</" : strKey : ">") ; Replace with value from ini file.
   EndWhile
Next

; Write buffer to disk file.
intBytesWritten = BinaryWrite (hdlBB, strFileXmlOut)

; Close buffer.
hdlBB = BinaryFree (hdlBB)

; If this script has been called from another WB script, then return to the caller from here ...
If RtStatus () != 10 Then If IntControl (77, 80, 0, 0, 0) > 0 Then Return ; ... when not in WinBatch Studio debug mode.

; Display result.
ShellExecute (strFileXmlOut, "", "", @NORMAL, "")

:CANCEL
Exit