批处理—从XML文件中获取标记值

时间:2022-11-27 17:19:28

I have this following XML file called "test.xml"

我有以下XML文件,名为“test.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>

4 个解决方案

#1


2  

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=%%#"

#2


1  

xmlstarlet sel -t -v "//ragent-name/text()" file.xml

Check http://xmlstar.sourceforge.net/

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

#3


1  

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 echoing it...

当然,您可以将结果分配给一个变量,而不是重复它……

#4


0  

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检验。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.

该方法允许以一种非常简单的方式从目标标记中提取位于上下或左右行的标记。唯一的麻烦是,mydumvar变量保留在找到令牌值的环境中。

Further details at this post.

详情请参阅本文。

#1


2  

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=%%#"

#2


1  

xmlstarlet sel -t -v "//ragent-name/text()" file.xml

Check http://xmlstar.sourceforge.net/

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

#3


1  

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 echoing it...

当然,您可以将结果分配给一个变量,而不是重复它……

#4


0  

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检验。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.

该方法允许以一种非常简单的方式从目标标记中提取位于上下或左右行的标记。唯一的麻烦是,mydumvar变量保留在找到令牌值的环境中。

Further details at this post.

详情请参阅本文。