使用Windows批处理解析XML文件

时间:2021-03-28 21:23:27

How can I extract an STRING like "US_NY" between the tags <LOCATION></LOCATION> from a XML file? I tried it with FINDSTR, but the line breaks are problematic.

如何从XML文件中的标签 之间提取“US_NY”之类的STRING?我用FINDSTR尝试过,但换行是有问题的。

<?xml version="1.0" encoding="utf-16"?>
<DEVICE>
    <AGENT>
        <VERSION>
            2.0.0.2
        </VERSION>
        <CONNECTION>
            <LOCATION>
                US_NY
            </LOCATION>
            <SERVERIP>
                127.0.0.1
            </SERVERIP>
            <TCPPORT>
                5656
            </TCPPORT>
            <POLLINTERVAL>
                5
            </POLLINTERVAL>
        </CONNECTION>
    </AGENT>
</DEVICE>

7 个解决方案

#1


7  

One more

多一个

@echo off
    setlocal enableextensions enabledelayedexpansion
    set "xmlFile=%~1"
    for /f "tokens=1,2 delims=:" %%n in ('findstr /n /i /c:"<LOCATION>" "%xmlFile%"') do (
        for /f "tokens=*" %%l in ('type "%xmlFile%" ^| more +%%n') do set "location=%%l" & goto endLoop
    )
:endLoop
    echo %location%

#2


13  

You should use XML.EXE within batch to read an XML file. For more details go to http://xmlstar.sourceforge.net/

您应该在批处理中使用XML.EXE来读取XML文件。有关更多详细信息,请访问http://xmlstar.sourceforge.net/

Batch File:

批处理文件:

@echo off
for /f %%i in ('XML.EXE sel -t -v "//LOCATION" CP.xml') do set var=%%i
echo LOCATION is %var%

output:

输出:

LOCATION is US_NY

#3


4  

If you want to use a helper batch file (by aacini) then this will work:

如果你想使用一个帮助程序批处理文件(由aacini),那么这将工作:

@echo off
for /f "tokens=*" %%a in ('findrepl /i "<location>" /e:"</location>" /o:+1:-1 ^< "file.xml" ') do echo "%%a"

This uses a helper batch file called findrepl.bat from - https://www.dropbox.com/s/rfdldmcb6vwi9xc/findrepl.bat

这使用了一个名为findrepl.bat的帮助程序批处理文件 - https://www.dropbox.com/s/rfdldmcb6vwi9xc/findrepl.bat

Place findrepl.bat in the same folder as the batch file.

将findrepl.bat放在与批处理文件相同的文件夹中。

#4


3  

Here's the xpath.bat -small script that will allow you to get an xml node/attribute value by xpath expression without using external binaries.

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

For your case it can be used like this:

对于您的情况,它可以像这样使用:

call xpath.bat  "location.xml" "//LOCATION"

or to assign the value to a variable:

或者将值赋给变量:

for /f "tokens=* delims=" %%a  in  ('xpath.bat  "location.xml" "//LOCATION"') do (
   set "location=%%a"
)

Pure batch solution

纯批量解决方案

 @echo off
        for /f "tokens=1 delims=:" %%L in ('findstr /n "<LOCATION>" some.xml') do ( 
         set begin_line=%%L
        )

        for /f "tokens=1 delims=:" %%L in ('findstr /n "</LOCATION>" some.xml') do ( 
         set /a end_line=%%L+1
        )

        echo showing lines between %end_line% and %begin_line%
        break>"%temp%\empty"
        for /f "delims=" %%l in ('fc "%temp%\empty" "some.xml" /lb  %end_line% /t ^|more +4 ^| findstr /B /E /V "*****" ^| more +%begin_line%') do (
         set "location=%%l"
         goto :break_for
        )
        :break_for
        echo %location%
        del /Q /F "%temp%\empty"

Replace some.xml with the name of your xml.

将some.xml替换为xml的名称。

#5


1  

Try this:

尝试这个:

@echo off
setlocal EnableDelayedExpansion
set lastLine=0
< input.xml (for /F "delims=:" %%a in (
              'findstr /N /C:"<LOCATION>" input.xml') do (
   set /A skip=%%a-lastLine+1, lastLine=%%a+2
   for /L %%i in (1,1,!skip!) do set /P line=
   set /P "line=!line!" & echo:
))

Note: the answer is an adaptation of the answer (probably given by @Aacini) on this forum post: Windows batch FindStr to search for a string and matching line.

注意:答案是在这个论坛帖子中修改答案(可能由@Aacini提供):Windows批量FindStr搜索字符串和匹配行。

#6


1  

Pure batch -

纯批次 -

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

where q19722041.xml is your source .xml file.

其中q19722041.xml是您的源.xml文件。

#7


0  

sed for Windows

用于Windows的sed

sed -n "/<LOCATION>/{n;p}" file.xml

in a batch file:

在批处理文件中:

for /f %%a in ('sed -n "/<LOCATION>/{n;p}" file.xml') do set "location=%%a"
echo(%location%

#1


7  

One more

多一个

@echo off
    setlocal enableextensions enabledelayedexpansion
    set "xmlFile=%~1"
    for /f "tokens=1,2 delims=:" %%n in ('findstr /n /i /c:"<LOCATION>" "%xmlFile%"') do (
        for /f "tokens=*" %%l in ('type "%xmlFile%" ^| more +%%n') do set "location=%%l" & goto endLoop
    )
:endLoop
    echo %location%

#2


13  

You should use XML.EXE within batch to read an XML file. For more details go to http://xmlstar.sourceforge.net/

您应该在批处理中使用XML.EXE来读取XML文件。有关更多详细信息,请访问http://xmlstar.sourceforge.net/

Batch File:

批处理文件:

@echo off
for /f %%i in ('XML.EXE sel -t -v "//LOCATION" CP.xml') do set var=%%i
echo LOCATION is %var%

output:

输出:

LOCATION is US_NY

#3


4  

If you want to use a helper batch file (by aacini) then this will work:

如果你想使用一个帮助程序批处理文件(由aacini),那么这将工作:

@echo off
for /f "tokens=*" %%a in ('findrepl /i "<location>" /e:"</location>" /o:+1:-1 ^< "file.xml" ') do echo "%%a"

This uses a helper batch file called findrepl.bat from - https://www.dropbox.com/s/rfdldmcb6vwi9xc/findrepl.bat

这使用了一个名为findrepl.bat的帮助程序批处理文件 - https://www.dropbox.com/s/rfdldmcb6vwi9xc/findrepl.bat

Place findrepl.bat in the same folder as the batch file.

将findrepl.bat放在与批处理文件相同的文件夹中。

#4


3  

Here's the xpath.bat -small script that will allow you to get an xml node/attribute value by xpath expression without using external binaries.

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

For your case it can be used like this:

对于您的情况,它可以像这样使用:

call xpath.bat  "location.xml" "//LOCATION"

or to assign the value to a variable:

或者将值赋给变量:

for /f "tokens=* delims=" %%a  in  ('xpath.bat  "location.xml" "//LOCATION"') do (
   set "location=%%a"
)

Pure batch solution

纯批量解决方案

 @echo off
        for /f "tokens=1 delims=:" %%L in ('findstr /n "<LOCATION>" some.xml') do ( 
         set begin_line=%%L
        )

        for /f "tokens=1 delims=:" %%L in ('findstr /n "</LOCATION>" some.xml') do ( 
         set /a end_line=%%L+1
        )

        echo showing lines between %end_line% and %begin_line%
        break>"%temp%\empty"
        for /f "delims=" %%l in ('fc "%temp%\empty" "some.xml" /lb  %end_line% /t ^|more +4 ^| findstr /B /E /V "*****" ^| more +%begin_line%') do (
         set "location=%%l"
         goto :break_for
        )
        :break_for
        echo %location%
        del /Q /F "%temp%\empty"

Replace some.xml with the name of your xml.

将some.xml替换为xml的名称。

#5


1  

Try this:

尝试这个:

@echo off
setlocal EnableDelayedExpansion
set lastLine=0
< input.xml (for /F "delims=:" %%a in (
              'findstr /N /C:"<LOCATION>" input.xml') do (
   set /A skip=%%a-lastLine+1, lastLine=%%a+2
   for /L %%i in (1,1,!skip!) do set /P line=
   set /P "line=!line!" & echo:
))

Note: the answer is an adaptation of the answer (probably given by @Aacini) on this forum post: Windows batch FindStr to search for a string and matching line.

注意:答案是在这个论坛帖子中修改答案(可能由@Aacini提供):Windows批量FindStr搜索字符串和匹配行。

#6


1  

Pure batch -

纯批次 -

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

where q19722041.xml is your source .xml file.

其中q19722041.xml是您的源.xml文件。

#7


0  

sed for Windows

用于Windows的sed

sed -n "/<LOCATION>/{n;p}" file.xml

in a batch file:

在批处理文件中:

for /f %%a in ('sed -n "/<LOCATION>/{n;p}" file.xml') do set "location=%%a"
echo(%location%