0

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?

4
  • 1
    Why does it have to be a batch file? Commented Feb 5, 2017 at 21:08
  • unfortunately , that is a requirement in my case, i.e it cannot be *.sh , however if there a good option in Perl or other scripting language I might be able to consider it. Commented Feb 5, 2017 at 21:32
  • Can't it be JavaScript? From what I remember Windows used to have this "scripting host" thing. Isn't that still there? Commented Feb 5, 2017 at 21:37
  • Yep, looks like WSH is available from Win98 onwards (Win10 still has it): en.wikipedia.org/wiki/Windows_Script_Host Commented Feb 5, 2017 at 21:49

2 Answers 2

1
@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.

Sign up to request clarification or add additional context in comments.

6 Comments

for some reason the code does'nt move past the "for loop". it exits out and prints test count= test fail count=
Batch syntax is very specific. It's best to cut-and-paste. If you were to add a line echo "%%~1 on a new line directly under :select, then you should see the routine examining each element provided in turn.
that worked. it was a typo on my part thanks a lot !
For some reason , the code isnt working again. I copy pasted into notepad++ coult that be the problem?
@mofi: The options parameter merely states that the string contains one or more keywords - although the examples use a space-separator. cmd appears to see whether the option is contained in the option string, deletes any option found from the string before checking for the next option. Hence "delims=#usebackq" is valid; usebackq is detected first & eliminated, # is used as delims. I habitually use the delims option last to circumvent typos. Would "delims=#usebackq" & "delims=# usebackq" act differently? I treat the space as optional & agree it's clearer but more typing.
|
0

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"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.