Home > Mobile >  How to get value from numbered xml line using windows command batch file
How to get value from numbered xml line using windows command batch file

Time:04-04

I would need get some list of DDAT by DLFN number and put them in some csv file For example device number (dlfn 1); device No. (dlfn 200); MAC address (dlfn 104) will be result: 1;11536852;00:11:6D:C1:4C:3F

<sx_data>
<sx_r><DLFN>1</DLFN><DBEZ>Device address                </DBEZ><DDAT>1</DDAT></sx_r>
<sx_r><DLFN>2</DLFN><DBEZ>Device name                   </DBEZ><DDAT>WOS00187DA15099</DDAT></sx_r>
<sx_r><DLFN>3</DLFN><DBEZ>Department no.                </DBEZ><DDAT>1</DDAT></sx_r>
<sx_r><DLFN>4</DLFN><DBEZ>Department no. PLU accees     </DBEZ><DDAT>1</DDAT></sx_r>
<sx_r><DLFN>5</DLFN><DBEZ>Department no. EAN access     </DBEZ><DDAT>1</DDAT></sx_r>
<sx_r><DLFN>100</DLFN><DBEZ>TDHCP                         </DBEZ><DDAT>Switched on                   </DDAT></sx_r>
<sx_r><DLFN>101</DLFN><DBEZ>IP address                    </DBEZ><DDAT>10.29.1.64</DDAT></sx_r>
<sx_r><DLFN>102</DLFN><DBEZ>Subnet screen                 </DBEZ><DDAT>255.255.255.0</DDAT></sx_r>
<sx_r><DLFN>103</DLFN><DBEZ>Gateway                       </DBEZ><DDAT>10.29.1.1</DDAT></sx_r>
<sx_r><DLFN>104</DLFN><DBEZ>MAC address                   </DBEZ><DDAT>00:11:6D:C1:4C:3F</DDAT></sx_r>
<sx_r><DLFN>200</DLFN><DBEZ>Device no.                    </DBEZ><DDAT>11536852</DDAT></sx_r>
<sx_r><DLFN>1401</DLFN><DBEZ>Software package version 26</DBEZ><DDAT>2.94</DDAT></sx_r>
<sx_r><DLFN>1402</DLFN><DBEZ>Software package build 26</DBEZ><DDAT>build.8</DDAT></sx_r>
</sx_data>

Is it possible to use windows batch file? If it is not enough, any other easier option will be nice. Thank you for your help

CodePudding user response:

Easy for one value: filter for the correct line and read the value. Easy to expand: use the code for one line as a subroutine and call it with parameters (here the number and a variable name to store the value):

@echo OFF
setlocal
call :getvalue 1 var1
call :getvalue 200 var2
call :getvalue 104 var3
echo %var1%;%var2%;%var3%

goto :eof
:getvalue
for /f "tokens=9 delims=<>" %%a in ('find "<DLFN>%1</DLFN>" test.txt') do set "%2=%%a"
goto :eof

Result with your example data:

1;11536852;00:11:6D:C1:4C:3F

As an alternative, instead of the call and echo lines, you can use:

for %%a in (1,200,104) do call :getvalue %%a var%%a
echo %var1%;%var200%;%var104%

(note the variable names. Might be more intuitive to maintain)

CodePudding user response:

Please use an XML parser (like for instance):

xidel -s input.xml -e "for $x in ('1','200','104') return //sx_r[DLFN=$x]/DDAT" --output-separator=,
1,11536852,00:11:6D:C1:4C:3F

xidel -s input.xml -e "join(for $x in ('1','200','104') return //sx_r[DLFN=$x]/DDAT,',')"
1,11536852,00:11:6D:C1:4C:3F
  • Related