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.
我尝试读取一个xml文件,并从xml文件的构建标记之间读取字符串“50”。我试过了,但没有得到任何输出。
The XML file..
XML文件. .
<?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???
如果xml文件和批处理文件位于同一个文件夹,并且我从cmd执行批处理文件,那么代码会运行吗?
EDIT 1:
编辑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.
@MC ND我做了您提到的修改并运行了代码。当我按回车键时,光标会移动到下一行,然后被困在那里。我也没有给出任何输出。我也无法关闭cmd窗口。所有这些都在下面的图像文件中解释。
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.
按照下面MCND的建议,我将文件重命名为findx。bat返回值"50"这就是我想要的。正确输出的屏幕截图如下所示。
Thanks a lot @MCND!!
非常感谢@MCND ! !
4 个解决方案
#1
5
Retrieve only the required line (find
), and using the adecuated delimiters (<>
), tokenize the line to retrieve the required information
只检索所需的行(查找),并使用adecuated分隔符(<>),将行标记为检索所需信息。
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%
#2
1
you can try the xpath.bat - script that can get a value from xml file by given xpath:
您可以尝试使用xpath。bat——通过给定的xpath从xml文件中获取值的脚本:
call xpath.bat "build.xml" "\\build"
#3
0
This term will not fire because %%a
is not going to be just <BUILD>
这一项不会发生火灾,因为%%a不会仅仅是 <构建> 。
"%%a"=="<BUILD>"
Add this line to your code after the FOR line and run it to show you what is happening:
在FOR行之后将这一行添加到代码中,并运行它以显示正在发生的事情:
echo "%%a"
#4
0
...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:
…我确信您已经意识到,使用批处理解析XML可能不是最简单/最明智的做法。xmlstarlet和xidel这样的工具更适合这种情况:
xidel -q file.xml -e //build
...to save the buildnumber in a variable X and output it:
…在变量X中保存buildnumber并输出它:
@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? :-)
…如果BeniBela (xidel coder)能编写如下程序来直接设置环境变量,而不是像ouput那样生成set命令,那就更好了。那将是非常强大的(而且是短暂的)。贝尼省?你呢?:-)
xidel -q -cmd file.xml -e x:=//build
#1
5
Retrieve only the required line (find
), and using the adecuated delimiters (<>
), tokenize the line to retrieve the required information
只检索所需的行(查找),并使用adecuated分隔符(<>),将行标记为检索所需信息。
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%
#2
1
you can try the xpath.bat - script that can get a value from xml file by given xpath:
您可以尝试使用xpath。bat——通过给定的xpath从xml文件中获取值的脚本:
call xpath.bat "build.xml" "\\build"
#3
0
This term will not fire because %%a
is not going to be just <BUILD>
这一项不会发生火灾,因为%%a不会仅仅是 <构建> 。
"%%a"=="<BUILD>"
Add this line to your code after the FOR line and run it to show you what is happening:
在FOR行之后将这一行添加到代码中,并运行它以显示正在发生的事情:
echo "%%a"
#4
0
...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:
…我确信您已经意识到,使用批处理解析XML可能不是最简单/最明智的做法。xmlstarlet和xidel这样的工具更适合这种情况:
xidel -q file.xml -e //build
...to save the buildnumber in a variable X and output it:
…在变量X中保存buildnumber并输出它:
@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? :-)
…如果BeniBela (xidel coder)能编写如下程序来直接设置环境变量,而不是像ouput那样生成set命令,那就更好了。那将是非常强大的(而且是短暂的)。贝尼省?你呢?:-)
xidel -q -cmd file.xml -e x:=//build