Page 1 of 1
Replace text in XML file - Problem with !
Posted: 23 Feb 2015 18:15
by TBQ
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!
)
Re: Replace text in XML file - Problem with !
Posted: 23 Feb 2015 18:36
by foxidrive
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.
Re: Replace text in XML file - Problem with !
Posted: 23 Feb 2015 20:33
by TBQ
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?
Re: Replace text in XML file - Problem with !
Posted: 23 Feb 2015 20:46
by Aacini
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
Re: Replace text in XML file - Problem with !
Posted: 23 Feb 2015 22:27
by TBQ
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!