[英]Batch - get tag value from XML file
I have this following XML file called "test.xml"我有这个名为“test.xml”的XML文件
how can I get "Agent-name-8181818" value from "ragent-name" tag into a variable within a batch file?如何从“ragent-name”标签中获取“Agent-name-8181818”值到批处理文件中的变量中?
<ragent>
<type>ragent</type>
<logger>
<loglevel>log_warning</loglevel>
<logger-dir>.</logger-dir>
</logger>
<additional-config>
<logger-level>log_warning</logger-level>
</additional-config>
<configuration-info ts="26-02-2018_15-31-54">
<apply-config-now>false</apply-config-now>
<manual-settings-activation>Automatic</manual-settings-activation>
<ragent-name ts="26-02-2018_15-31-54">Agent-name-8181818</ragent-name>
<site ts="26-02-2018_15-31-54">site</site>
</configuration-info>
</ragent>
With xpath.bat (does not require external binaries , just uses built-in windows capabilities): 使用xpath.bat (不需要外部二进制文件,仅使用内置的Windows功能):
call xpath test.xml "*//configuration-info/ragent-name"
to assign result to a variable: 将结果分配给变量:
for /f "tokens=* delims=" %%# in ('call xpath test.xml "*//configuration-info/ragent-name"') do set "reagent=%%#"
xmlstarlet sel -t -v "//ragent-name/text()" file.xml
As a batch-file line, 作为批处理文件行,
FOR /f "tokens=3delims=<>" %%a IN ('findstr "ragent-name" "q49011270.txt"') DO echo %%a
Where q49011270.txt
is the file containing your data. 其中
q49011270.txt
是包含您的数据的文件。
Naturally, you could assign the result to a variable instead of echo
ing it... 自然地,您可以将结果分配给变量,而不是
echo
它。
You may solve this and other similar problems using the lookup capabilities of SETX command: 您可以使用SETX命令的查找功能解决此问题以及其他类似问题:
for /F "tokens=3" %%a in ('setx /F test.xml dummyVar /R 0^,2 ragent-name /D "<>"') do set "ragent=%%a" & goto continue
:continue
echo %ragent:~0,-1%
In this command: 在此命令中:
setx /F test.xml dummyVar /R 0^,2 ragent-name /D "<>"
This method allows to extract tokens in lines above or below, or at left or right, from a target token in a very simple way. 此方法允许以一种非常简单的方式从目标令牌的上方或下方或左侧或右侧的行中提取令牌。 The only inconvenience is that the dummyVar variable remains in the environment with the value of the token found.
唯一的不便之处在于, dummyVar变量将与找到的令牌值一起保留在环境中。
FOR /f "tokens=2 delims=>< " %%a IN ('TYPE config.xml ^| FIND ""') DO SET CONNECTION_STRING=%%a FOR /f "tokens=2 delims=><" %%a IN ('TYPE config.xml ^| FIND ""') DO SET CONNECTION_STRING=%%a
config.xml is the path of the files connectionString is tag id connection_string is the variable name which you want to assign1 config.xml 是文件的路径 connectionString 是标签 ID connection_string 是您要分配的变量名称1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.