WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

XML
plus
plus

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

XML Search and Replace

 Keywords: xml search replace wildcard match text file StrIndexWild FileGet FilePu IniItemize ItemCount ItemExtract StrReplace StrLenWild Binary Tag BinaryTagInit BinaryTagFind BinaryTagRepl
DOM Extender DOMParseFile WxDOM44i.dll XMLDOM Msxml2 Microsoft.XMLDOM.1.0 Msxml2.DOMDocument.6.0 GetElementsByTagName Regular Expression RegExp VBScript.RegExp pattern

Question:

I’m trying to develop a script that will pass through a large XML file and replace some data with new values from a text file (maybe called something like myconfig.ini). Every quarter I submit some data to a government agency, but need to strip out some of the items which we aren’t required to submit. The XML file has a bunch of entries of varying lengths, so a straight BinaryReplace won’t do the trick since you need to know what you’re specifically replacing. For example, one of the elements, D04, might have entries like Red and Green, etc. and I want to replace them all with Not Applicable if the line in the myconfig.ini file for D04 says D04 = Not Applicable. The myconfig.ini file would have entries like:
D03 = Report not required;
D04 = Not Applicable;
D05 = PASS (which I want to leave the value alone and pass it through). 
I tried StrSubWild to get the values like Red and Green to put into BinaryReplace, but it doesn’t work. I read through the ADO and XML Primer and just can’t come up with the right strategy for recursively passing through the file to replace multiple values with what it gets from the myconfig.ini file.
  <?xml Version="1.0" encoding="utf-8" ?>
- <EMSDataSet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
- <Header>
  <D01_01>36215</D01_01>
  <D01_03>06</D01_03>
  <D01_04>06071</D01_04>
  <D01_07>6110</D01_07>
  <D01_08>5810</D01_08>
  <D01_09>5870</D01_09>
  <D01_19>5970</D01_19>
  <D01_21>1234567890</D01_21>
  <D02_07>91786</D02_07>
- <Record>
- <C01>
  <C01_01>062</C01_01>
  <C01_02>856</C01_02>
  <C01_03>9002</C01_03>
  <C01_04>40</C01_04>
  <C01_04>85</C01_04>
  <C01_04>65</C01_04>
  <C01_04>60</C01_04>
  <C01_04>55</C01_04>
  <C01_05>-25 - Not Applicable</C01_05>
  </C01>
- <E01>
  <E01_01>201104143710LWWJF23072</E01_01>
  <E01_02>ZOLLDATASYSTEMS</E01_02>
  <E01_03>FireRMS</E01_03>
  <E01_04>4.7</E01_04>
  </E01>
- <E02>
  <E02_01>36215</E02_01>
  <E02_02>0000001</E02_02>
  <E02_03>-15</E02_03>
  <E02_04>30</E02_04>
  <E02_05>60</E02_05>
  <E02_06>105</E02_06>
  <E02_07>150</E02_07>
  <E02_08>225</E02_08>
  <E02_09>-10</E02_09>
  <E02_10>360</E02_10>
  <E02_11>-15</E02_11>
  <E02_12>ME161</E02_12>
  <E02_13>-15</E02_13>
  <E02_14>-15</E02_14>
  <E02_20>390</E02_20>
  </E02>
- <E03>
  <E03_01>-10</E03_01>
  <E03_02>-10</E03_02>
  <E03_03>-15</E03_03>
  </E03>
- <E05>
  <E05_02>2008-125-01T17:00:30Z</E05_02>
  <E05_03>2008-125-01T17:00:30Z</E05_03>
  <E05_04>2008-125-01T17:00:30Z</E05_04>
  <E05_05>2008-125-01T17:00:30Z</E05_05>
  <E05_06>2008-125-01T17:05:00Z</E05_06>
  <E05_07>2008-125-01T17:06:00Z</E05_07>
  <E05_09>2008-125-01T18:00:00Z</E05_09>
  <E05_10 xsi:nil="true" />
  <E05_11>2008-125-01T18:00:00Z</E05_11>
  <E05_13>2008-125-01T18:00:00Z</E05_13>
  </E05>
- <E06>
- <E06_01_0>
  <E06_01>Smith</E06_01>
  <E06_02>Samuel</E06_02>
  <E06_03>S</E06_03>
  </E06_01_0>
- <E06_04_0>
  <E06_04>123 N. Main Street</E06_04>
  <E06_07>06</E06_07>
  <E06_08>91786</E06_08>
  </E06_04_0>
  <E06_09>-15</E06_09>
  <E06_10>-20</E06_10>
  <E06_11>650</E06_11>
  <E06_12>670</E06_12>
  <E06_13>695</E06_13>
- <E06_14_0>
  <E06_14>60</E06_14>
  <E06_15>715</E06_15>
  </E06_14_0>
  <E06_16>1950-05-01</E06_16>
  <E06_17>9091234567</E06_17>
- <E06_19_0>
  <E06_18>-20</E06_18>
  <E06_19>-20</E06_19>
  </E06_19_0>
  </E06>
- <E07>
  <E07_01>-10</E07_01>
  <E07_02>-15</E07_02>
  <E07_15>-20</E07_15>
  <E07_16>-15</E07_16>
  <E07_17>-15</E07_17>
  <E07_32>-15</E07_32>
  <E07_33>-15</E07_33>
  <E07_34>1015</E07_34>
- <E07_35_0>
  <E07_35>8010</E07_35>
  <E07_36>786.05</E07_36>
  </E07_35_0>
- <E07_35_0>
  <E07_35>8063</E07_35>
  <E07_36>-5</E07_36>
  </E07_35_0>
- <E07_35_0>
  <E07_35>8001</E07_35>
  <E07_36>789.00</E07_36>
  </E07_35_0>
- <E07_35_0>
  <E07_35>8005</E07_35>
  <E07_36>796.4</E07_36>
  </E07_35_0>
- <E07_35_0>
  <E07_35>8050</E07_35>
  <E07_36>869.0</E07_36>
  </E07_35_0>
  <E07_37>-15</E07_37>
  </E07>
- <E08>
  <E08_01>-15</E08_01>
  <E08_02>-15</E08_02>
  <E08_03>-15</E08_03>
  <E08_05>1130</E08_05>
  <E08_06>0</E08_06>
  <E08_07>1160</E08_07>
  <E08_08>-15</E08_08>
  <E08_09>-15</E08_09>
- <E08_11_0>
  <E08_11>123 N Birch Ave</E08_11>
  <E08_14>06</E08_14>
  <E08_15>91786</E08_15>
  </E08_11_0>
  </E08>
- <E09>
  <E09_01>-20</E09_01>
  <E09_02>-20</E09_02>
  <E09_03>-20</E09_03>
  <E09_04>1</E09_04>
  <E09_05>pain In abdomen</E09_05>
  <E09_06_0 />
  <E09_08>-15</E09_08>
  <E09_09_0 />
  45
  <E09_11>1305</E09_11>
  <E09_12>1370</E09_12>
  <E09_13>1475</E09_13>
  <E09_14>1570</E09_14>
  <E09_15>1617</E09_15>
  <E09_16>1765</E09_16>
  </E09>
- <E10>
  <E10_01>9595</E10_01>
  <E10_02>-20</E10_02>
  <E10_03>2035</E10_03>
  <E10_04>-20</E10_04>
  <E10_05>-20</E10_05>
- <E10_06_0>
  <E10_07>-20</E10_07>
  </E10_06_0>
  <E10_08>-20</E10_08>
  <E10_09>-20</E10_09>
  </E10>
- <E11>
  <E11_01>0</E11_01>
  <E11_02>-25</E11_02>
  <E11_03>-25</E11_03>
  <E11_04>-25</E11_04>
  <E11_05>-25</E11_05>
  <E11_06>-25</E11_06>
  <E11_07>-15</E11_07>
  <E11_08>-25</E11_08>
  <E11_10>-25</E11_10>
  <E11_11>-25</E11_11>
  </E11>
- <E12>
  <E12_01>2615</E12_01>
  <E12_02>-15</E12_02>
  <E12_03>-15</E12_03>
- <E12_4_0>
  <E12_06>-15</E12_06>
  <E12_04>-15</E12_04>
  <E12_05>-15</E12_05>
  </E12_4_0>
  <E12_07>-20</E12_07>
  <E12_08>-20</E12_08>
  <E12_09>-20</E12_09>
  <E12_10>-20</E12_10>
  <E12_11>-20</E12_11>
- <E12_14_0>
  <E12_15_0 />
  <E12_17>-20</E12_17>
  </E12_14_0>
  <E12_18>-15</E12_18>
  <E12_19>2990</E12_19>
  <E12_20>-25</E12_20>
  </E12>
- <E13>
  <E13_01>-5</E13_01>
  </E13>
- <E16>
  <E16_02>-20</E16_02>
  </E16>
- <E17>
  <E17_01>-15</E17_01>
  </E17>
- <E18>
  <E18_01>2008-125-01T17:11:00Z</E18_01>
  <E18_02>0</E18_02>
  <E18_03>8730</E18_03>
  <E18_04>4205</E18_04>
  <E18_05_0 />
  <E18_07>4375</E18_07>
  <E18_08>-20</E18_08>
  <E18_08>4390</E18_08>
  <E18_09>-20</E18_09>
  <E18_10>-20</E18_10>
  <E18_11>-20</E18_11>
  </E18>
- <E18>
  <E18_01>2008-125-01T17:10:00Z</E18_01>
  <E18_02>0</E18_02>
  <E18_03>8655</E18_03>
  <E18_04>4205</E18_04>
  <E18_05_0 />
  <E18_07>4380</E18_07>
  <E18_08>-20</E18_08>
  <E18_08>4405</E18_08>
  <E18_09>-20</E18_09>
  <E18_10>-20</E18_10>
  <E18_11>-20</E18_11>
  </E18>
- <E19>
- <E19_01_0>
  <E19_01>2008-125-01T17:06:00Z</E19_01>
  <E19_02>0</E19_02>
  <E19_03>96.700</E19_03>
  <E19_04>-20</E19_04>
  <E19_05>1</E19_05>
  <E19_06>1</E19_06>
  <E19_07>-20</E19_07>
  <E19_07>4500</E19_07>
  <E19_07>-20</E19_07>
  <E19_08>4600</E19_08>
  <E19_09>-20</E19_09>
  <E19_10>-20</E19_10>
  <E19_11>-20</E19_11>
  </E19_01_0>
- <E19_01_0>
  <E19_01>2008-125-01T17:07:00Z</E19_01>
  <E19_02>0</E19_02>
  <E19_03>89.700</E19_03>
  <E19_04>-20</E19_04>
  <E19_05>1</E19_05>
  <E19_06>1</E19_06>
  <E19_07>-20</E19_07>
  <E19_07>4500</E19_07>
  <E19_07>-20</E19_07>
  <E19_08>4605</E19_08>
  <E19_09>-20</E19_09>
  <E19_10>-20</E19_10>
  <E19_11>-20</E19_11>
  </E19_01_0>
- <E19_01_0>
  <E19_01>2008-125-01T17:12:00Z</E19_01>
  <E19_02>0</E19_02>
  <E19_03>100.200</E19_03>
  <E19_04>-20</E19_04>
  <E19_05>1</E19_05>
  <E19_06>1</E19_06>
  <E19_07>4500</E19_07>
  <E19_07>-20</E19_07>
  <E19_08>4605</E19_08>
  <E19_09>-20</E19_09>
  <E19_10>-20</E19_10>
  <E19_11>-20</E19_11>
  </E19_01_0>
- <E19_01_0>
  <E19_01>2008-125-01T17:08:00Z</E19_01>
  <E19_02>0</E19_02>
  <E19_03>93.591</E19_03>
  <E19_04>-20</E19_04>
  <E19_05>1</E19_05>
  <E19_06>1</E19_06>
  <E19_07>-20</E19_07>
  <E19_07>-20</E19_07>
  <E19_07>4500</E19_07>
  <E19_08>4605</E19_08>
  <E19_09>-20</E19_09>
  <E19_10>-20</E19_10>
  <E19_11>-20</E19_11>
  </E19_01_0>
- <E19_01_0>
  <E19_01>2008-125-01T17:09:00Z</E19_01>
  <E19_02>0</E19_02>
  <E19_03>38.992</E19_03>
  <E19_04>-20</E19_04>
  <E19_05>1</E19_05>
  <E19_06>1</E19_06>
  <E19_07>-20</E19_07>
  <E19_07>4500</E19_07>
  <E19_08>4605</E19_08>
  <E19_09>-20</E19_09>
  <E19_10>-20</E19_10>
  <E19_11>-20</E19_11>
  </E19_01_0>
  </E19>
- <E20>
  <E20_01>-20</E20_01>
  <E20_02>"-10"</E20_02>
- <E20_03_0>
  <E20_03>-20</E20_03>
  <E20_05>06</E20_05>
  <E20_07>-15</E20_07>
  </E20_03_0>
  <E20_09>-15</E20_09>
  <E20_10>4851</E20_10>
  <E20_11>-15</E20_11>
  <E20_12>-15</E20_12>
  <E20_13>-15</E20_13>
  <E20_14>-10</E20_14>
  <E20_15>-10</E20_15>
  <E20_16>-10</E20_16>
  <E20_17>5080</E20_17>
  </E20>
- <E22>
  <E22_01>-10</E22_01>
  <E22_02>-10</E22_02>
  <E22_03>-15</E22_03>
  <E22_04>-20</E22_04>
  <E22_05>0000037</E22_05>
  <E22_06>-15</E22_06>
  </E22>
- <E23>
  <E23_01>-15</E23_01>
  <E23_02>-15</E23_02>
  <E23_03>-15</E23_03>
  <E23_04>-15</E23_04>
  <E23_05>-15</E23_05>
  <E23_06>-15</E23_06>
  <E23_07>-15</E23_07>
  <E23_08>-15</E23_08>
- <E23_09_0>
  <E23_09>-15</E23_09>
  <E23_11>-15</E23_11>
  </E23_09_0>
  <E23_10>-15</E23_10>
  </E23>
  </Record>
  </Header>
  </EMSDataSet>

Answer:

There are a number of ways to accomplish this, see examples below. Based on testing here are the Performance Contest results:
; -----------------------------------------------------------------
; Performance Contest Result
; Iterations = 100
; Test  Ticks   Pct  Contestant
;
;                    The 'StrIndexWild' Version.
;    1  73027  37.4  20111216.XML.MassReplace.1.wbt
;
;                    The 'Binary Buffer' Version.
;    2  49953  25.6  20111218.XML.MassReplace.2.wbt
;
;                    The 'Document Object Model (DOM) Extender' Version.
;    3  51922  26.6  20111219.XML.MassReplace.3.wbt
;
;                    The 'Microsoft XML DOM Object' Version.
;    4  12817   6.6  20111220.XML.MassReplace.4.wbt
;
;                    The 'Regular Expression' Version.
;    5   7780   4.0  20111220.XML.MassReplace.5.wbt  <== The Winner
;
; DateTime = 2011:12:20:13:18:32
; -----------------------------------------------------------------


'StrIndexWild' Version.

For this simple search and replace within a text file you can use simple technology and a simple script like the following ...
; DD.20111216.
;-----------------------------------------------------
;   Ini file "Config.ini".
;
;   [IniFilePass]
;   C01_01=Pass
;   C01_02=Pass
;   C01_03=Pass
;   C01_04=Pass
;   E01_01=Pass
;   D01_02=Pass
;   D01_03=Not Reporting
;   D01_04=Not Reporting
;   D01_05=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
;-----------------------------------------------------

; strFolderHome = "C:\LEMSA\"
strFolderHome = DirScript ()
DirChange (strFolderHome)

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)

; Read input xml into string variable.
strXml = FileGet (strFileXmlIn)

; 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, "", strFileIni)
   If strValue == "" Then Continue
   If strValue == "Pass" Then Continue

   strTagSearch = StrReplace ("<{1}>*</{1}>", "{1}", strKey)
   ;strTagReplace = StrReplace ("<{1}>### SECRET DATA ###</{1}>", "{1}", strKey) ; Adapt it to your needs, for example leave xml tag content empty.
   strTagReplace = StrReplace ("<{1}>" : strValue : "</{1}>", "{1}", strKey) ; Adapt it to your needs, for example fill it with the value from ini file.

   intPos = 1
   While @TRUE
      intPos = StrIndexWild (strXml, strTagSearch, intPos)
      If intPos == 0 Then Break
      intLen = StrLenWild (strXml, strTagSearch, intPos)
      strXml = StrSub (strXml, 1, intPos - 1) : strTagReplace : StrSub (strXml, intPos + intLen, -1)
      intPos = intPos + StrLen (strTagReplace)
   EndWhile
Next

intBytesWritten = FilePut (strFileXmlOut, strXml)

ShellExecute (strFileXmlOut, "", "", @NORMAL, "")

:CANCEL
Exit


'Binary Buffer' Version.

Sometimes a XML file can have a rather big file size (see specific articles in the Tech Data Base), therefore it can be appropriate to use WinBatch binary buffer functions. Here is the binary buffer version ...
; DD.20111218.
;
; XML Mass Replace.
; The Binary Buffer Version.
;-----------------------------------------------------
;   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
;-----------------------------------------------------

; strFolderHome = "C:\LEMSA\"
strFolderHome = DirScript ()
DirChange (strFolderHome)

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)

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

:CANCEL
Exit


'Document Object Model (DOM) Extender' Version.

; DD.20111219.
;
; XML Mass Replace.
; The 'Document Object Model (DOM) Extender' Version.
;-----------------------------------------------------
;   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
;-----------------------------------------------------

; Load the 3rdParty DOM extender, made by Jennifer Palonus, Graphical Dynamics, http://autointern.bestmessageboard.com
AddExtender ("WxDOM44i.dll")


; strFolderHome = "C:\LEMSA\"
strFolderHome = DirScript ()
DirChange (strFolderHome)

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)


; Read XML file into DOM buffer.
hdlDocXml = DOMParseFile (strFileXmlIn, @DOMDocXML, @DOMAuto)

; 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

   ; Do the replacements.
   arrNodes = Arrayize (DOMGetElementsByTagname (hdlDocXml, strKey), @TAB)
   intNodeLast = ArrInfo (arrNodes, 1) - 1
   For intNode = 0 To intNodeLast
      blnResult = DOMSetInnerHTML (arrNodes[intNode], strValue, @TRUE)
   Next
Next

; Write DOM buffer to disk file.
blnResult = DOMWriteFile (hdlDocXml, strFileXmlOut, @DOMAuto)

; Close DOM buffer.
hdlDocXml = DOMClose (hdlDocXml)

; 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


'Microsoft XML DOM Object' Version.

; DD.20111219.
;
; XML Mass Replace.
; The 'Microsoft XML DOM Object' Version.
;-----------------------------------------------------
;   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
;-----------------------------------------------------

; Load the Microsoft XML DOM Object Interface.
;objDocXml = ObjectCreate ("Microsoft.XMLDOM.1.0", "")
objDocXml = ObjectCreate ("Msxml2.DOMDocument.6.0", "")


; strFolderHome = "C:\LEMSA\"
strFolderHome = DirScript ()
DirChange (strFolderHome)

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)


; Read XML file into DOM buffer.
blnResult = objDocXml.load(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

   ; Do the replacements.
   ForEach objNode In objDocXml.getElementsByTagName(strKey)
      objNode.text = strValue
   Next
Next

; Write DOM buffer to disk file.
objDocXml.save(strFileXmlOut)

; Close objects.
objNode = 0
objDocXml = 0

; 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


'Regular Expression' Version.

; DD.20111220.
;
; XML Mass Replace.
; The 'Regular Expression' Version.
;-----------------------------------------------------
;   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
;-----------------------------------------------------

; strFolderHome = "C:\LEMSA\"
strFolderHome = DirScript ()
DirChange (strFolderHome)

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)

; Create Regular Expression object interface and define settings.
objRegExp = ObjectCreate ("VBScript.RegExp")
objRegExp.IgnoreCase = @TRUE
objRegExp.Global = @TRUE
objRegExp.MultiLine = @TRUE

; Read input xml into string variable.
strXml = FileGet (strFileXmlIn)

; 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

   ; Do the replacements.
   objRegExp.Pattern = "<" : strKey : ">.*?</" : strKey : ">"
   strXml = objRegExp.Replace(strXml, : "<" : strKey : ">" : strValue : "</" : strKey : ">")
Next

; Write string variable to disk file.
intBytesWritten = FilePut (strFileXmlOut, strXml)

; Close object.
objRegExp = 0

; 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

Article ID:   W18494
Filename:   XML Search and Replace.txt
File Created: 2011:12:20:10:14:32
Last Updated: 2011:12:20:10:14:32