I\'m trying to read an xml file and read STRING \"50\" between the build tags from a XML file. I tried it but I\'m not getting any output.
The XML file..
Retrieve only the required line (find
), and using the adecuated delimiters (<>
), tokenize the line to retrieve the required information
delims: v v v v
line : <Build>50</Build>
tokens:^1 ^2 ^3 ^4
Now, translate this into code
@echo off
setlocal enableextensions disabledelayedexpansion
set "build="
for /f "tokens=3 delims=<>" %%a in (
'find /i "<Build>" ^< "file.xml"'
) do set "build=%%a"
echo %build%
...I'm sure you're aware that parsing XML with batch might not be the easiest/smartest thing to do. Tools like xmlstarlet and xidel are better suited for this:
xidel -q file.xml -e //build
...to save the buildnumber in a variable X and output it:
@echo off
for /f "delims=" %%a in ('xidel -q --output-format cmd file.xml -e "x:=//build"') do %%a
echo Build version = %x%
...even nicer would be if BeniBela (xidel coder) could program something like the following to directly set the environment variables and not generate a set command as ouput. That would be very powerful (and short). Beni? You're up for this? :-)
xidel -q -cmd file.xml -e x:=//build
This term will not fire because %%a
is not going to be just <BUILD>
"%%a"=="<BUILD>"
Add this line to your code after the FOR line and run it to show you what is happening:
echo "%%a"
you can try the xpath.bat - script that can get a value from xml file by given xpath:
call xpath.bat "build.xml" "\\build"