Here is a pure batch-file solution. The following script expects three command line arguments: the (path to the) XML file, the node (slideshow
in your example) and the attribute name (date
), in the given order. The return data is output to the console window and can be redirected into a file (>
).
This is a sample command line to run the script (supposing the XML file is called presentation.xml
, the script is named extract-xml-attrib.bat
and the resulting data are to be written to attr.txt
):
"extract-xml-attrib.bat" "presentation.xml" "slideshow" "date" > "attr.txt"
This is the code of the script extract-xml-attrib.bat
:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "XML=%~1"
set "TAG=%~2"
set "PAR=%~3"
rem // Define defaults here:
if not defined XML set "XML=presentation.xml"
if not defined TAG set "TAG=slideshow"
if not defined PAR set "PAR=date"
set "FLAG="
for /F usebackq^ delims^=^ eol^= %%L in ("%XML%") do (
set "LINE=%%L"
setlocal EnableDelayedExpansion
set "REST=!LINE:*<%TAG%=!"
if not defined REST (
set "FLAG=#"
) else (
set "REST=!LINE:*<%TAG% =!"
if not "!REST!"=="!LINE!" (
set "FLAG=#"
) else (
set "REST=!LINE:*<%TAG% =!"
if not "!REST!"=="!LINE!" (
set "FLAG=#"
)
)
)
for /F "tokens=1,2 delims=>" %%E in ("!REST!/") do (
if defined FLAG (
endlocal
set "FLAG=#"
) else (
endlocal
)
set "REST=%%E"
if defined FLAG (
call :GET_ATTR "REST:~,-1" "%PAR%"
)
if not "%%F"=="" (
set "FLAG="
)
setlocal EnableDelayedExpansion
)
endlocal
)
endlocal
exit /B
:GET_ATTR var_string param_name
setlocal DisableDelayedExpansion
set "PAR=%~2"
setlocal EnableDelayedExpansion
set "STR=!%~1!"
set "NEXT="
for %%S in (!STR!) do (
if defined NEXT (
endlocal
echo(%%~S
set "NEXT="
setlocal EnableDelayedExpansion
)
set "STR=!STR:*%%S=!"
if "%%S"=="%PAR%" (
if defined STR (
if "!STR:~,1!"=="=" (
if "!STR:~1,1!"==" " (
echo(
) else if "!STR:~1,1!"==" " (
echo(
) else (
set "NEXT=#"
)
)
)
)
)
if defined NEXT echo(
endlocal
endlocal
exit /B