XML Parser and Demo Infinite Tokens


By: nbrane
October 13, 2012

The problem of isolating xml tags and data using batch is not easy to solve reliably. This is my feeble attempt to do so. It does, however, demonstrate a means of extracting an unlimited and indefinited number of elements from each line by incrementing the "tokens=" values in a loop. It is also capable of handling "!" since delayed expansion was not used. Double-quotes will cause failure, and probably some other characters as well.

 
::===== begin script  
@echo off > xml.out & setlocal  
:: parse xml into tags and content  
 
for /f "tokens=*" %%z in (%1.xml) do (  
set test=%%z  
set z=1  
set y=2  
call :aaa  
)  
echo done  
goto :eof  
 
:aaa  
for /f "tokens=%z%,%y% delims=<" %%a in ("%test%") do (  
set f=%%b  
set s=%%a  
call :bild  
)  
if "%f%" equ "" set z=0 & set y=0 & goto :eof  
set /a z+=1  
set /a y+=1  
goto :aaa  
 
:bild  
set stest=%s:>=%  
for /f "tokens=1,2 delims=>" %%c in ("%s%") do (  
if "%stest%" equ "%s%" (>> xml.out echo DATA:%%a) else (  
>> xml.out echo TAG:%%c DATA:%%d  
)  
)  
::========= end


Need more help?
Describe your Problem
Example: Hard Drive Not Detected on My PC

Ask Question