Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse XML file with windows batch

Tags:

xml

batch-file

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>
like image 903
eichhorn Avatar asked Nov 01 '13 09:11

eichhorn


People also ask

How do you parse an XML file?

To parse XML documents, use the XML PARSE statement, specifying the XML document that is to be parsed and the processing procedure for handling XML events that occur during parsing, as shown in the following code fragment.

What does %1 do in batch?

When used in a command line, script, or batch file, %1 is used to represent a variable or matched string. For example, in a Microsoft batch file, %1 can print what is entered after the batch file name.

What is the best way to parse XML in Java?

DOM Parser is the easiest java xml parser to learn. DOM parser loads the XML file into memory and we can traverse it node by node to parse the XML. DOM Parser is good for small files but when file size increases it performs slow and consumes more memory.

Can browser parse XML?

All major browsers have a built-in XML parser to access and manipulate XML.


1 Answers

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.

like image 129
Magoo Avatar answered Nov 15 '22 20:11

Magoo