使用Windows批处理解析XML文件

时间:2013-11-01 09:28:22

标签: xml batch-file

如何从XML文件中的标记<LOCATION></LOCATION>之间提取类似“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 个答案:

答案 0 :(得分:15)

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

批处理文件:

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

<强>输出:

LOCATION is US_NY

答案 1 :(得分:7)

再一次

@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 :(得分:4)

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

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

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

或将值赋给变量:

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

纯批次解决方案

 @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"

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

答案 3 :(得分:4)

如果您想使用帮助程序批处理文件(通过aacini),那么这将起作用:

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

这使用了来自 - https://www.dropbox.com/s/rfdldmcb6vwi9xc/findrepl.bat

的名为findrepl.bat的帮助程序批处理文件

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

答案 4 :(得分:2)

纯批次 -

@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

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

答案 5 :(得分:1)

试试这个:

@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:
))

注意:答案是对此论坛帖子的回答(可能由@Aacini提供):Windows batch FindStr to search for a string and matching line

答案 6 :(得分:0)

sed for Windows

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

在批处理文件中:

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