computing
  • 1

Solved Read Attribute Value From Xml Through Batch Script

  • 1

Suppose,in my testfile.xml i have ,
<head ip=”123″ />
and if i have to fetch the value of ip i.e, 123 through batch scrpiting..
i have tried the code and i am not getting the value 123 in my output
this is the code i have written in test.bat
@echo on
for /F “usebackq tokens=*” %%I IN (‘findstr /l “head ip=” C:\Temp\testfile.xml’)
do call :readxml %%I
:readxml
set var1=%1
echo %var1%

I am new to Batch scripting please correct me if I am wrong in the code.

Thanks in Advance

Share

1 Answer

  1. xml is always tedious/difficult/frustrating to process using batch, but here goeth the treacherous and futile assault thereon:

    @echo off & setlocal enabledelayedexpansion
    for /f “tokens=*” %%a in (‘findstr /i “head ip=” test.xml’) do (
    set z=%%a
    set z=!z:^<head ip=!
    :get rid of the equals sign
    set z!z!
    set z=!z: /^>=!
    call :readxml !z!
    )
    goto :eof
    :readxml
    echo in xml…
    for %%a in (%1) do set test=%%~a
    echo %test%
    :end

    • 0