简体   繁体   English

批处理 - 从 XML 文件中获取标签值

[英]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

检查http://xmlstar.sourceforge.net/

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 "<>"
  • /F test.xml is the file to process. / F test.xml是要处理的文件。
  • dummyVar is a required variable. dummyVar是必需变量。
  • /R 0,2 ragent-name specify to search for "ragent-name" token in the file and, after found, return the token placed in the same line (0) and 2 tokens to right. / R 0,2 ragent-name指定在文件中搜索“ ragent-name”令牌,找到后,将返回位于同一行(0)的令牌和右侧的2个令牌。
  • /D "<>" indicate that tokens delimiters are "<" and ">" characters, besides spaces and tabs. / 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变量将与找到的令牌值一起保留在环境中。

Further details at this post . 这个职位的进一步细节。

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

With the XML/HTML/JSON-parser :使用 XML/HTML/JSON 解析器

FOR /F "delims=" %A IN ('
  xidel -s test.xml -e "var:=//ragent-name" --output-format^=cmd
') DO %A

Output: Output:

SET var=Agent-name-8181818

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM