本文介绍了使用Windows批处理读取XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试读取xml文件,并从XML文件的构建标记之间读取STRING50。我尝试了,但我没有得到任何输出。

I'm trying to read an xml file and read STRING "50" between the build tags from a XML file. I tried it but I'm not getting any output.

XML文件..

    <?xml version="1.0" encoding="UTF-8"?>
    <CDMDataXML xmlns="http://www.avocent.org/trellis/CDMLoaderXMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.avocent.org/trellis/CDMLoaderXMLSchema CDMLoaderXMLSchema.xsd">
<CDMDataVersion>
    <Major>1</Major>
    <Minor>0</Minor>
    <Build>50</Build>
    <Delimiter>.</Delimiter>
</CDMDataVersion>

批处理文件代码..

@ECHO OFF
SETLOCAL
SET "build="&SET "grab="
FOR /f "tokens=*" %%a IN (version.xml) DO (
IF DEFINED grab SET build=%%a&SET "grab="
IF /i "%%a"=="<BUILD>" SET grab=Y
)
ECHO found build=%build%
GOTO :EOF

如果xml文件和批处理文件位于同一文件夹中并且我从cmd执行批处理文件,代码将运行

Will the code run if the xml file and the batch file are situated in the same folder and i execute the batch file from cmd???

编辑1 :

@MC ND我做了你提到的更改并运行了代码。当我输入,光标移动到下一行,被卡在那里我也没有给出任何输出。我也无法关闭cmd窗口。所有这些都在下面的图片文件中解释。

@MC ND I made the changes you mentioned and ran the code.When i hit enter, the cursor moves to the next line and gets stuck there.I doesn't give any output as well. I'm not able to close the cmd window also. All this is explained in the image file below.

ANSWER

findx.bat,它返回值50,这是我想要的。正确输出的屏幕截图如下所示。

As suggested by MCND below I renamed my file to findx.bat which is returning the value "50" which is what i wanted.The screen-shot of the correct output is given below.

非常感谢@MCND !!

Thanks a lot @MCND!!

推荐答案

只检索所需的行( find ),并使用自动分隔符c><> ),将该行标记为检索所需的信息

Retrieve only the required line (find), and using the adecuated delimiters (<>), tokenize the line to retrieve the required information

     delims:    v     v  v      v
     line  :    <Build>50</Build>
     tokens:^1   ^2    ^3 ^4

现在, / p>

Now, translate this into code

@echo off
    setlocal enableextensions disabledelayedexpansion

    set "build="
    for /f "tokens=3 delims=<>" %%a in (
        'find /i "<Build>" ^< "file.xml"'
    ) do set "build=%%a"

    echo %build%

这篇关于使用Windows批处理读取XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 21:26