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!!
This term will not fire because
%%a
is not going to be just<BUILD>
Add this line to your code after the FOR line and run it to show you what is happening:
Retrieve only the required line (
find
), and using the adecuated delimiters (<>
), tokenize the line to retrieve the required informationNow, translate this into code
you can try the xpath.bat - script that can get a value from xml file by given xpath:
...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:
...to save the buildnumber in a variable X and output it:
...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? :-)