Parse XML file with windows batch

后端 未结 7 1346
感动是毒
感动是毒 2020-12-01 21:26

How can I extract an STRING like \"US_NY\" between the tags from a XML file? I tried it with FINDSTR, but the line breaks are

相关标签:
7条回答
  • 2020-12-01 22:07

    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%
    
    0 讨论(0)
  • 2020-12-01 22:08

    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%
    
    0 讨论(0)
  • 2020-12-01 22:09

    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.

    0 讨论(0)
  • 2020-12-01 22:10

    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
    
    0 讨论(0)
  • 2020-12-01 22:14

    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.

    0 讨论(0)
  • 2020-12-01 22:16

    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.

    0 讨论(0)
提交回复
热议问题