Read XML file with windows batch

后端 未结 4 600
北荒
北荒 2021-01-23 08:09

I\'m trying to read an xml file and read STRING \"50\" between the build tags from a XML file. I tried it but I\'m not getting any output.

The XML file..



        
相关标签:
4条回答
  • 2021-01-23 08:33

    Retrieve only the required line (find), and using the adecuated delimiters (<>), tokenize the line to retrieve the required information

         delims:    v     v  v      v
         line  :    <Build>50</Build>
         tokens:^1   ^2    ^3 ^4
    

    Now, translate this into code

    @echo off
        setlocal enableextensions disabledelayedexpansion
    
        set "build="
        for /f "tokens=3 delims=<>" %%a in (
            'find /i "<Build>" ^< "file.xml"'
        ) do set "build=%%a"
    
        echo %build%
    
    0 讨论(0)
  • 2021-01-23 08:33

    ...I'm sure you're aware that parsing XML with batch might not be the easiest/smartest thing to do. Tools like xmlstarlet and xidel are better suited for this:

    xidel -q file.xml -e //build
    

    ...to save the buildnumber in a variable X and output it:

    @echo off
    for /f "delims=" %%a in ('xidel -q --output-format cmd file.xml -e "x:=//build"') do %%a
    echo Build version = %x%
    

    ...even nicer would be if BeniBela (xidel coder) could program something like the following to directly set the environment variables and not generate a set command as ouput. That would be very powerful (and short). Beni? You're up for this? :-)

    xidel -q -cmd file.xml -e x:=//build
    
    0 讨论(0)
  • 2021-01-23 08:43

    This term will not fire because %%a is not going to be just <BUILD>

    "%%a"=="<BUILD>" 
    

    Add this line to your code after the FOR line and run it to show you what is happening:

    echo "%%a"
    
    0 讨论(0)
  • 2021-01-23 08:56

    you can try the xpath.bat - script that can get a value from xml file by given xpath:

    call xpath.bat "build.xml" "\\build"
    
    0 讨论(0)
提交回复
热议问题