Reputation: 293
I have an XML file (generated by a third party tool) which is of format
<?xml version="1.0" encoding="UTF-8"?><ROOT test_count="22" test_fail_count="1" test_pass_count="21".......</ROOT>
All the content is in one line of the file with name Report.xml
.
I have been trying to write a batch script
@echo off
setlocal EnableDelayedExpansion
set "Report="
for /f "delims=" %%x in (Report.xml) do set "Report=!Report! %%x"
REm set /p Report=< %Results_File%
echo report is !Report!
call:parseVal !Report!
exit/b
:parseVal
setlocal enableDelayedExpansion
set val="%~1"
echo %val%
echo !val!|findstr /rc:"test_count=.[0-9]*." >nul || (
echo !val!
EndLocal
exit /b
)
rem ..
Basically I am trying to grab values for test_count(22)
, test_fail_count(1)
and test_pass_count(21)
from the XML file.
Any ideas on how to achieve this?
Upvotes: 0
Views: 738
Reputation:
Provided the structure and order of Report.xml is constant this could do (cmd line)
for /f "tokens=5,6 delims=<> " %A in (Report.xml) Do @Set %A&Set %B
Sample output with your Report.xml
> set test
test_count="22"
test_fail_count="1"
Upvotes: 0
Reputation: 80023
@ECHO Off
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "filename1=%sourcedir%\q42057260.txt"
SET "testcount="
SET "testfailcount="
FOR /f "usebackqdelims=" %%a IN ("%filename1%") DO (
SET "xmlline=%%a"
CALL :process
)
ECHO test count=%testcount% test fail count=%testfailcount%
GOTO :EOF
:process
:: dispose of awkward characters
SET "xmlline=%xmlline:?= %"
SET "xmlline=%xmlline:>= %"
SET "xmlline=%xmlline:<= %"
CALL :select %xmlline%
GOTO :EOF
:select
IF /i "%~1"=="" GOTO :EOF
IF DEFINED testcount IF DEFINED testfailcount GOTO :EOF
IF /i "%~1"=="test_count" SET /a testcount=%~2
IF /i "%~1"=="test_fail_count" SET /a testfailcount=%~2
SHIFT
GOTO select
GOTO :EOF
You would need to change the setting of sourcedir
to suit your circumstances.
I used a file named q42057260.txt
containing your data for my testing.
Read the file to %%a
and transfer to xmlline
.
Use :process
to remove awkward characters (those with a special meaning to cmd
) by replacing each with a space, then pass the resultant line to :select
as a parameter-list.
look at each parameter in turn, stripping quotes. When the target strings appear, set their storage variables to the following value. When both values are assigned, bail out.
and report results.
Upvotes: 1