11

如何在<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>
4

7 回答 7

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
于 2013-11-01T10:31:23.410 回答
8

多一个

@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%
于 2013-11-01T10:40:25.123 回答
5

这是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 的名称。

于 2013-11-01T10:35:19.753 回答
4

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

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

findrepl.bat这使用从 - https://www.dropbox.com/s/rfdldmcb6vwi9xc/findrepl.bat调用的帮助程序批处理文件

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

于 2013-11-01T10:55:05.057 回答
3

纯批次——

@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文件。

于 2013-11-01T12:19:51.987 回答
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 批处理 FindStr to search a string and matching line

于 2013-11-01T10:25:10.367 回答
0

sed 用于 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%
于 2013-11-01T14:00:59.727 回答