解析XML文件,从批处理文件属性 [英] Parse XML file for attribute from batch file

查看:366
本文介绍了解析XML文件,从批处理文件属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我解析XML文件象下面这样:

 <?XML版本=1.0&GT?;
<! -
- >
<结构>
   <设置>
      <连接GT&;
            <连接名称=名称1值=连接1TYPE =ABC/>
            <连接名称=NAME2值=连接2TYPE =高清/>
      < /连接>
   < /设置>
< /结构>

从批处理文件,我提示连接名称的用户。我想解析XML获得具有指定名称的连接,并得到其数值。因此,如果用户提供了名称1,我要选择连接1。我只好从下面的code <一个href=\"http://stackoverflow.com/questions/13902744/extract-xml-tag-values-based-on-a-flag-using-batch\">Extract XML标签值(基于标志)使用批处理

我不熟悉的(特别是划,代币)循环批处理文件,所以我不知道这是如何工作以及如何使其为我工作。

 (FOR / F令牌= 1,2 delims ==%%一中(CONNECTION.CONFIG)做(
   如果%%〜BNEQ,设置一个%% = %%一B
   如果/我!名字! EQU%名称%回声!值!
))


解决方案

它的工作原理,如果你使用正确的令牌和分隔符:

 关闭@echo和放大器; SETLOCAL
FOR / F标记^ = 2 ^ 3 ^,^ 5delims = ^&LT; ^^ = %%一中(CONNECTION.CONFIG)做(
   如果%%一个EQU连接名称回声(%% b %%Ç

输出是:

 名1连接1
NAME2连接2

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>

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

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
)

Output is:

name1 connection1
name2 connection2

这篇关于解析XML文件,从批处理文件属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆