从批处理文件中解析属性的XML文件

时间:2022-11-27 17:24:15

I am parsing a XML file like below:

我正在解析如下的XML文件:

<?xml version="1.0"?>
<!--
-->
<configuration>
   <settings>
      <connections>
            <connection name="name1" value="connection1" type="abc"/>
            <connection name="name2" value="connection2" type="def"/>
      </connections>
   </settings>
</configuration>

From the batch file, I prompt the user for connection name. I want to parse the XML get a connection with the specified name and get its value. So If user gives name1, I want to select connection1. I had the below code from Extract XML Tag Values (Based on a Flag) Using Batch

从批处理文件中,我提示用户输入连接名称。我想解析XML获取具有指定名称的连接并获取其值。所以如果用户给出name1,我想选择connection1。我使用批处理提取XML标记值(基于标记)下面的代码

I am not familiar with for loop in (especially delimits, tokens) batch file, so I am not sure how this works and how to make it work for me.

我不熟悉for循环(特别是分隔符,令牌)批处理文件,所以我不确定这是如何工作的以及如何使它适合我。

(for /F "tokens=1,2 delims== " %%a in (connection.config) do (
   if "%%~b" neq "" set %%a=%%~b
   if /I "!name!" equ "%name%" echo !value!
))

3 个解决方案

#1


3  

It works, if you use the right tokens and delimiters:

如果您使用正确的令牌和分隔符,它可以工作:

@echo off&setlocal
for /F tokens^=2^,3^,5delims^=^<^"^= %%a in (connection.config) do (
   if "%%a" equ "connection name" echo(%%b %%c
)

Output is:

name1 connection1
name2 connection2

#2


2  

@ECHO OFF
SETLOCAL
SET "name=name1"
SET "connection="
SET "type="

for /F "tokens=5,7delims==/ " %%a in (
 'findstr /c:"<connection name=\"%name%\"" ^<connection.config'
 ) do SET connection=%%~a&SET type=%%~b

ECHO connection=%connection%
ECHO TYPE      =%type%

Finding the data line which contains the literal string "\" escapes ") then set connection to the 5th (and type for good measure) from the seventh token of the data line

查找包含文字字符串“\”的数据行“\ n”转义“)然后从数据行的第七个标记设置连接到第5个(并且为良好度量键入)

        <connection name="name1" value="connection1" type="abc"/>

using =, / and [space] as delimiters.

使用=,/和[space]作为分隔符。

#3


0  

Here's the xpath.bat -small script that will allow you to get a xml values by xpath expression without using external binaries:

这是xpath.bat -small脚本,它允许您通过xpath表达式获取xml值,而无需使用外部二进制文件:

call xpath.bat "connection.config" "//connection/@name"
call xpath.bat "connection.config" "//connection/@value"

#1


3  

It works, if you use the right tokens and delimiters:

如果您使用正确的令牌和分隔符,它可以工作:

@echo off&setlocal
for /F tokens^=2^,3^,5delims^=^<^"^= %%a in (connection.config) do (
   if "%%a" equ "connection name" echo(%%b %%c
)

Output is:

name1 connection1
name2 connection2

#2


2  

@ECHO OFF
SETLOCAL
SET "name=name1"
SET "connection="
SET "type="

for /F "tokens=5,7delims==/ " %%a in (
 'findstr /c:"<connection name=\"%name%\"" ^<connection.config'
 ) do SET connection=%%~a&SET type=%%~b

ECHO connection=%connection%
ECHO TYPE      =%type%

Finding the data line which contains the literal string "\" escapes ") then set connection to the 5th (and type for good measure) from the seventh token of the data line

查找包含文字字符串“\”的数据行“\ n”转义“)然后从数据行的第七个标记设置连接到第5个(并且为良好度量键入)

        <connection name="name1" value="connection1" type="abc"/>

using =, / and [space] as delimiters.

使用=,/和[space]作为分隔符。

#3


0  

Here's the xpath.bat -small script that will allow you to get a xml values by xpath expression without using external binaries:

这是xpath.bat -small脚本,它允许您通过xpath表达式获取xml值,而无需使用外部二进制文件:

call xpath.bat "connection.config" "//connection/@name"
call xpath.bat "connection.config" "//connection/@value"