13

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>
3
  • What is the logic to determine what needs to be extracted? Commented Nov 1, 2013 at 9:53
  • I want to get the locations from somde devices. This information is stored in different XML files. Therefore I need to extract the STRING or line between the xml tag <location> and </location>. This should be done with windows batch script. Commented Nov 1, 2013 at 10:07
  • possible duplicate of A batch file to extract the value of a specific XML tag Commented Nov 1, 2013 at 10:19

7 Answers 7

15

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
Sign up to request clarification or add additional context in comments.

Comments

9

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%

3 Comments

@lucy, Look at the third line. xmlFile is assigned the file name passed as argument to the batch file. So, yes, it is the name of the file.
using findstr has a major problem because it makes the command rely on xml file formatting. If you put multiple tags on the same line it fails to match the pattern!!
@Greenish, i agree, xml handling from batch scripts should be done with something like xmlstartlet, but a) it is a non native solution (a problem for some environments) b) the problem in the OP question is the line breaks.
6

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.

Comments

5

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.

2 Comments

Clever dual-usage script, both CMD and JScript using built-in CScript. It even re-use itself to type the help if need be.
findrepl.bat is no longer available for future generations.
3

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.

Comments

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.

Comments

0

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%

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.