使用 Windows 批处理解析 XML 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19724021/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Parse XML file with windows batch
提问by eichhorn
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.
如何<LOCATION></LOCATION>从 XML 文件的标签之间提取像“US_NY”这样的字符串?我用 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>
采纳答案by MC ND
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%
回答by Navin Rawat
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
回答by foxidrive
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.batfrom - https://www.dropbox.com/s/rfdldmcb6vwi9xc/findrepl.bat
这使用findrepl.bat从 - https://www.dropbox.com/s/rfdldmcb6vwi9xc/findrepl.bat调用的帮助程序批处理文件
Place findrepl.batin the same folder as the batch file.
将findrepl.bat在同一文件夹中的批处理文件。
回答by npocmaka
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.xmlwith the name of your xml.
替换some.xml为您的 xml 的名称。
回答by Magoo
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 .xmlfile.
其中 q19722041.xml 是您的源.xml文件。
回答by Alex Filipovici
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 batch FindStr to search for a string and matching line。
回答by Endoro
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%

