问题描述
我正在解析如下的 XML 文件:
I am parsing a XML file like below:
<?xml version="1.0"?>
<!--
-->
<configuration>
<settings>
<connections>
<connection name="name1" value="connection1" type="abc"/>
<connection name="name2" value="connection2" type="def"/>
</connections>
</settings>
</configuration>
从批处理文件中,我提示用户输入连接名称.我想解析 XML 获取具有指定名称的连接并获取其值.所以如果用户给出name1,我想选择connection1.我从 Extract XML Tag Values (Based在标志上)使用批处理
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
我不熟悉批处理文件中的 for 循环(尤其是分隔符、标记),所以我不确定它是如何工作的,以及如何让它为我工作.
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 /F "tokens=1,2 delims== " %%a in (connection.config) do (
if "%%~b" neq "" set %%a=%%~b
if /I "!name!" equ "%name%" echo !value!
))
推荐答案
如果你使用正确的标记和分隔符,它可以工作:
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
)
输出是:
name1 connection1
name2 connection2
这篇关于从批处理文件中解析 XML 文件的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!