Can you help me to list browsers from this file http://techpatterns.com/downloads/firefox/useragentswitcher.xml into txt file, separated by %tab% delimiter?
3 or 4 c
Check the xpath.bat - script that can get values from xml by given xpath expression:
call xpath.bat "useragentswitcher.xml" "//folder/@description"
Answer to your comment How should I use double quotes as delimiter?
Simply use the form
FOR /F tokens^=1^,2^ delims^=^" %%B IN ("%%A") DO
How this works?
Normally you can't use a quote character as a delim character.
This is the only known workaround, the important thing is that the normal quotes arround the FOR/F options are missing.
But it's neccessary that the options are parsed as only one token, therefore you need to escape all standard batch-parser delimiters (space tab =,;).
The quote isn't a batch delimiter, but it need to be escaped too, to avoid that the rest of the line is quoted, then the parser would fail.
But you could change the ^"
with ""
as the second quote will be ignored.
FOR /F tokens^=1^,2^ delims^="" %%B IN ("%%A") DO ...
It seems you ought to be able to find a much better tool than batch to parse XML...
But I believe the code below is what you are looking for.
Because the number of folders varies, I swapped the order of the columns in the output. I put the browser description first, followed by the folders, one per column. This allows the definition of each column to be fixed.
I used the info in jeb's answer to include "
as a FOR delimiter.
EDIT - I simplified the code
Note - This first attempt was written to work with a copy of the XML that was retrieved using Internet Explorer. I've since discovered that IE altered the format of the file. This code is highly dependent on the exact format of the file, so it will not work on the original XML. It also serves as an example as to why batch is a poor choice for parsing XML
@echo off
setlocal enableDelayedExpansion
::Define the files to use - change as needed
set input="test.xml"
set output="result.txt"
::The assignment below should have exactly one TAB character between = and "
set "TAB= "
set cnt=0
set "folder0="
>%output% (
for /f usebackq^ tokens^=1^,2^ delims^=^=^" %%A in (%input%) do (
for %%N in (!cnt!) do (
if "%%A"=="- <folder description" (
set /a cnt+=1
for %%M in (!cnt!) do set "folder%%M=!folder%%N!%TAB%%%B"
)
if "%%A"==" </folder>" (
set /a cnt-=1
)
if "%%A"==" <useragent description" (
echo %%B!folder%%N!
)
)
)
)
The code will fail if !
appears in any of the descriptions because delayed expansion will corrupt expansion of any FOR variable that contains !
. I checked, and your file does not contain !
in any description.
The code could be modified to handle !
in the description, but it would get more complicated. It requires toggling of delayed expansion on and off, and preservation of variable values across the ENDLOCAL barrier.
The above code is highly dependent on the format of the XML. It will fail if the non-standard dashes are removed, or if the white space arrangement changes.
The following variation is a bit more robust, but it still requires that each line contains exactly one XML tag.
@echo off
setlocal enableDelayedExpansion
::Define the files to use - change as needed
set input="test.xml"
set output="result.txt"
::The assignment below should have exactly one TAB character between = and "
set "TAB= "
set cnt=0
set "folder0="
>%output% (
for /f usebackq^ tokens^=1^,2^ delims^=^=^" %%A in (%input%) do (
for %%N in (!cnt!) do (
set "test=%%A"
if "!test:<folder description=!" neq "!test!" (
set /a cnt+=1
for %%M in (!cnt!) do set "folder%%M=!folder%%N!%TAB%%%B"
)
if "!test:</folder>=!" neq "!test!" (
set /a cnt-=1
)
if "!test:<useragent description=!" neq "!test!" (
echo %%B!folder%%N!
)
)
)
)
EDIT - One last version
Here is a version that can handle !
in the data. I've added an additional column to the output. The first column is still the browser description. The 2nd column is the useragent string. The remaining columns are the folders. The solution uses the delayed expansion toggling technique. It also uses an additional FOR /F to preserve a variable value across the ENDLOCAL barrier.
@echo off
setlocal disableDelayedExpansion
::Define the files to use - change as needed
set input="test.xml"
set output="result.txt"
::The assignment below should have exactly one TAB character between = and "
set "TAB= "
set cnt=0
set folder0=""
>%output% (
for /f usebackq^ tokens^=1-4^ delims^=^=^" %%A in (%input%) do (
set "test=%%A"
set "desc=%%B"
set "agent=%%D"
setlocal enableDelayedExpansion
for %%N in (!cnt!) do (
if "!test:<folder description=!" neq "!test!" (
set /a cnt+=1
for %%M in (!cnt!) do for /f "delims=" %%E in ("!folder%%N!") do (
endlocal
set "folder%%M=%%~E%TAB%%%B"
set "cnt=%%M"
)
) else if "!test:</folder>=!" neq "!test!" (
endlocal
set /a cnt-=1
) else if "!test:<useragent description=!" neq "!test!" (
echo !desc!%TAB%!agent!!folder%%N!
endlocal
) else endlocal
)
)
)