Reputation: 1852
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..
<?xml version="1.0" encoding="UTF-8"?>
<CDMDataXML xmlns="http://www.avocent.org/trellis/CDMLoaderXMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.avocent.org/trellis/CDMLoaderXMLSchema CDMLoaderXMLSchema.xsd">
<CDMDataVersion>
<Major>1</Major>
<Minor>0</Minor>
<Build>50</Build>
<Delimiter>.</Delimiter>
</CDMDataVersion>
The batch file code..
@ECHO OFF
SETLOCAL
SET "build="&SET "grab="
FOR /f "tokens=*" %%a IN (version.xml) DO (
IF DEFINED grab SET build=%%a&SET "grab="
IF /i "%%a"=="<BUILD>" SET grab=Y
)
ECHO found build=%build%
GOTO :EOF
Will the code run if the xml file and the batch file are situated in the same folder and i execute the batch file from cmd???
EDIT 1:
@MC ND I made the changes you mentioned and ran the code.When i hit enter, the cursor moves to the next line and gets stuck there.I doesn't give any output as well. I'm not able to close the cmd window also. All this is explained in the image file below.
ANSWER
As suggested by MCND below I renamed my file to findx.bat which is returning the value "50" which is what i wanted.The screen-shot of the correct output is given below.
Thanks a lot @MCND!!
Upvotes: 3
Views: 22796
Reputation: 57252
you can try the xpath.bat - script that can get a value from xml file by given xpath:
call xpath.bat "build.xml" "\\build"
Upvotes: 2
Reputation: 321
...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
Upvotes: 1
Reputation: 70923
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%
Upvotes: 8
Reputation: 41224
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"
Upvotes: 0