eichhorn
eichhorn

Reputation: 185

Parse XML file with windows batch

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

Upvotes: 13

Views: 72066

Answers (7)

npocmaka
npocmaka

Reputation: 57252

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.

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.

Upvotes: 6

Endoro
Endoro

Reputation: 37569

sed for Windows

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%

Upvotes: 0

Magoo
Magoo

Reputation: 79983

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.

Upvotes: 3

foxidrive
foxidrive

Reputation: 41224

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

@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

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

Upvotes: 5

MC ND
MC ND

Reputation: 70923

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%

Upvotes: 9

Navin Rawat
Navin Rawat

Reputation: 3138

You should use XML.EXE within batch to read an XML file. For more details go to 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

Upvotes: 15

Alex Filipovici
Alex Filipovici

Reputation: 32541

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.

Upvotes: 1

Related Questions