Here is a pure batch-file solution (let us call it change-version.bat
):
@echo off
setlocal EnableExtensions DisableDelayedExpansion
rem Define constants here:
set "XML_FILE=.\sample.xml"
set "TAG_NAME=tag"
set "PAR_NAME=versionNumber"
set "PAR_NEW_VAL="3.2.1""
for /F usebackq^ delims^=^ eol^= %%L in ("%XML_FILE%") do (
set "LINE=%%L"
for /F "tokens=1,* delims=< eol=<" %%D in ("%%L") do (
set "PRE=%%D" & set "LIN=%%E"
if not defined LIN (set "PRE=" & set "LIN=%%D")
setlocal EnableDelayedExpansion
for /F "tokens=1,* eol= " %%F in ("!LIN!") do (
setlocal DisableDelayedExpansion
set "INT=%%F" & set "TAG=%%G"
setlocal EnableDelayedExpansion
if /I "!INT!"=="%TAG_NAME%" (
for /F "tokens=1,* delims=> eol=>" %%T in ("!TAG!") do (
setlocal DisableDelayedExpansion
set "TAG=%%T" & set "END=%%U"
call :SUB NEW TAG
setlocal EnableDelayedExpansion
echo(!PRE!^<%TAG_NAME%!NEW!^>!END!
endlocal
endlocal
)
) else (
echo(!LINE!
)
endlocal
endlocal
)
endlocal
)
)
endlocal
exit /B
:SUB var_new var_tag
set "%~1="
:LOOP
setlocal EnableDelayedExpansion
for /F "tokens=1,* eol= " %%P in ("!%~2!") do (
endlocal
for /F "tokens=1,* delims== eol==" %%V in ("%%P") do (
if /I "%%V"=="%PAR_NAME%" (
set "PAR=%%V=%PAR_NEW_VAL%"
) else (
set "PAR=%%V=%%W"
)
)
set "%~2=%%Q"
setlocal EnableDelayedExpansion
)
for /F delims^=^ eol^= %%S in ("!%~1! !PAR!") do (
endlocal & set "%~1=%%S"
)
if defined %~2 goto :LOOP
exit /B
For this to work, the following conditions for the XML data must be fulfilled:
- the line that holds the tag
<tag>
does not contain any other XML tags;
- the tag
<tag>
containing the parameter versionNumber
must not be spanned over multiple lines;
- a parameter definition must not contain white-spaces around the
=
sign;
- the (quoted) parameter values must not contain
<
nor >
characters;
- both tag and parameter names are treated case-insensitively;
The script parses the XML file sample.xml
in the current directory.
To write the result to a file rather than to the console, redirect it like this:
"change-version.bat" > "result.xml"