Parse XML file for attribute from batch file

后端 未结 3 945
傲寒
傲寒 2021-01-13 03:05

I am parsing a XML file like below:




   
      
                 


        
相关标签:
3条回答
  • 2021-01-13 03:09
    @ECHO OFF
    SETLOCAL
    SET "name=name1"
    SET "connection="
    SET "type="
    
    for /F "tokens=5,7delims==/ " %%a in (
     'findstr /c:"<connection name=\"%name%\"" ^<connection.config'
     ) do SET connection=%%~a&SET type=%%~b
    
    ECHO connection=%connection%
    ECHO TYPE      =%type%
    

    Finding the data line which contains the literal string "\" escapes ") then set connection to the 5th (and type for good measure) from the seventh token of the data line

            <connection name="name1" value="connection1" type="abc"/>
    

    using =, / and [space] as delimiters.

    0 讨论(0)
  • 2021-01-13 03:16

    Here's the xpath.bat -small script that will allow you to get a xml values by xpath expression without using external binaries:

    call xpath.bat "connection.config" "//connection/@name"
    call xpath.bat "connection.config" "//connection/@value"
    

    to assign this to a variable:

    for /f "tokens=* delims=" %%# in ('xpath.bat "connection.config" "//connection/@value"') do set "connection_value=%%#"
    echo %connection_value%
    
    0 讨论(0)
  • 2021-01-13 03:23

    It works, if you use the right tokens and delimiters:

    @echo off&setlocal
    for /F tokens^=2^,3^,5delims^=^<^"^= %%a in (connection.config) do (
       if "%%a" equ "connection name" echo(%%b %%c
    )
    

    Output is:

    name1 connection1
    name2 connection2
    
    0 讨论(0)
提交回复
热议问题