Parse XML file with windows batch
You should use XML.EXE within batch to read an XML file. For more details go to http://xmlstar.sourceforge.net/
Batch File:
@echo offfor /f %%i in ('XML.EXE sel -t -v "//LOCATION" CP.xml') do set var=%%iecho LOCATION is %var%
output:
LOCATION is US_NY
One more
@echo off setlocal enableextensions enabledelayedexpansion set "xmlFile=%~1" for /f "tokens=1,2 delims=:" %%n in ('findstr /n /i /c:"<LOCATION>" "%xmlFile%"') do ( for /f "tokens=*" %%l in ('type "%xmlFile%" ^| more +%%n') do set "location=%%l" & goto endLoop ):endLoop echo %location%
Here's the xpath.bat -small script that will allow you to get an xml node/attribute value by xpath expression without using external binaries.
For your case it can be used like this:
call xpath.bat "location.xml" "//LOCATION"
or to assign the value to a variable:
for /f "tokens=* delims=" %%a in ('xpath.bat "location.xml" "//LOCATION"') do ( set "location=%%a")
Pure batch solution
@echo off for /f "tokens=1 delims=:" %%L in ('findstr /n "<LOCATION>" some.xml') do ( set begin_line=%%L ) for /f "tokens=1 delims=:" %%L in ('findstr /n "</LOCATION>" some.xml') do ( set /a end_line=%%L+1 ) echo showing lines between %end_line% and %begin_line% break>"%temp%\empty" for /f "delims=" %%l in ('fc "%temp%\empty" "some.xml" /lb %end_line% /t ^|more +4 ^| findstr /B /E /V "*****" ^| more +%begin_line%') do ( set "location=%%l" goto :break_for ) :break_for echo %location% del /Q /F "%temp%\empty"
Replace some.xml
with the name of your xml.