Replace text in XML file - Problem with !

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
TBQ
Posts: 7
Joined: 15 May 2014 16:33

Replace text in XML file - Problem with !

#1 Post by TBQ » 23 Feb 2015 18:15

I am writing a script to drive a 3rd party command line utility that requires an XML input/settings file for one of its commands. I need to replace a few simple values in the XML file each time it is run, and I'm building a temporary XML file to use based on a simple model settings file with the specified values. This works OK with a simple batch script - EXCEPT for XML comments (<!-- ... -->) and worse the <!DOCTYPE ...> statement. The "!" get stripped from the output, and I cannot figure out what sequence of escapes to use (or what other trick to apply).

Since the same setup will be used for different values, I think I'll run into the same issue trying to use XSLT. I'd like to avoid using VBE/CScript, PowerShell, Java, etc. I'd really like to make this work with a batch script...

My script (slightly simplified):

Code: Select all

@ECHO Off
SET MyVar=%1

for /f "tokens=* delims= " %%G in (%CD%\MyTemplate.xml) do (
   set str=%%G
   set str=!str:MyVar=%MyVar%!
   :: Handle "!" - at least for <!-- and <!DOCTYPE ... ???
   >> %CD%\newfile.xml echo !str!
)

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Replace text in XML file - Problem with !

#2 Post by foxidrive » 23 Feb 2015 18:36

Delayed expansion strips the !

There are various ways to echo into a file but the actual task will dictate the best approach to take.

Using findrepl.bat and jrepl.bat are two excellent ways to modify an existing file to change items inside it.

You can find them both in this site. Search using google for an easier search.

TBQ
Posts: 7
Joined: 15 May 2014 16:33

Re: Replace text in XML file - Problem with !

#3 Post by TBQ » 23 Feb 2015 20:33

I'd like to do this with batch only (no variation of other scripting), and I don't actually want to make the change in the original file.

The actual task is creating an XML file instance that contains values specified as input to the batch file (as command line parameters, environment variables, value pairs in a non-xml input file, etc.). The XML file is static except for the specific values passed in.

Is there no escape method, repeated "!", CALL, or other way to do this? What other options are there?

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Replace text in XML file - Problem with !

#4 Post by Aacini » 23 Feb 2015 20:46

Code: Select all

@ECHO Off

setlocal DisableDelayedExpansion
SET MyVar=%1

(for /F "delims=" %%G in (%CD%\MyTemplate.xml) do (
   set "str=%%G"
   setlocal EnableDelayedExpansion
   set "str=!str:MyVar=%MyVar%!"
   echo !str!
   endlocal
)) > %CD%\newfile.xml

Antonio

TBQ
Posts: 7
Joined: 15 May 2014 16:33

Re: Replace text in XML file - Problem with !

#5 Post by TBQ » 23 Feb 2015 22:27

Thanks @Aacini.

That took care of the !s.

I had to play around with blank lines. I had to make sure the template.xml had at least a space on an otherwise blank line (otherwise it would be skipped), and to trim blanks and check for an empty line, I ended up with

Code: Select all

@ECHO Off

setlocal DisableDelayedExpansion
SET MyVar=%1

(for /F "delims=" %%G in (%CD%\MyTemplate.xml) do (
   set "str=%%G"
   setlocal EnableDelayedExpansion
   set "strTrim={x}!str!{x}"
   if "!strTrim!"=="{x}{x}" (
      CALL :BlankLine
   ) ELSE (
      set "str=!str:MyVar=%MyVar%!"
      IF "!str!"=="" (
         CALL :BlankLine
      ) ELSE (      
         set "strTrim=!strTrim:   ={x}!"
         set "strTrim=!strTrim: ={x}!"
         set "strTrim=!strTrim:{x}{x}={x}!"
         set "strTrim=!strTrim:{x}{x}={x}!"
         :: I had to repeat the set "strTrim=!strTrim:{x}{x}={x}!" several times to get rid of all combinations of blanks - I could also have edited the template, of course...
         IF "!strTrim!"=="{x}" (CALL :BlankLine) ELSE (ECHO !str!)
      )
   )
   endlocal
)) > %CD%\newfile.xml

goto :eof

:BlankLine
ECHO(
goto :eof




As an intermediate attempt, I had simply turned my XML template into a batch file with every line formatted as
ECHO ^<... %MyVar% ...^> >> %CD%\newfile.xml
(with the substitution variables inline where needed)
and blank lines as
ECHO( >> %CD%\newfile.xml

Ugly, but since the XML layout is static, it works, too...

But I like yours better!

Post Reply