http://techpatterns.com/downloads/firefox/useragentswitcher.xml
进入txt文件,由%tab%delimiter分隔?
应该有3或4列:
1)来自示例数据的文件夹描述:<文件夹描述=“浏览器 - Windows”>
2)来自示例数据的浏览器类型:< folder description =“Legacy Browsers”>
3)来自示例数据的用户代理:< useragent description =“Avant Browser 1.2”useragent =“Avant Browser / 1.2.789rel1(http://www.avantbrowser.com)”app 在这里,我看到第一个问题,因为某些浏览器不在文件夹< folder description =“Legacy Browsers”>“但在< separator />下
所以第一列应该定义系统,第二列是类型,第三列是浏览器.
@echo off Setlocal EnableDelayedExpansion SET file=useragentswitcher.xml SET delim=" FOR /F "tokens=* skip=1" %%F IN (!file!) DO ( REM echo %%F call :parse "%%F" > temp.txt FOR /F "tokens=1,2,3,4,5,6,7 skip=1 delims=" %%A IN (temp.txt) DO ( IF "%%A"=="folder" ( SET /A level=!level!+1 echo Level:!level! ) ELSE IF "%%A"=="/folder" ( SET /A level=!level!-1 echo Level:!level! ) echo A:%%A ) pause ) exit /b :parse Setlocal EnableDelayedExpansion SET A=%* REM REMOVE double paranthesis and <> SET A=!A:~2,-2! REM replace double qoutes SET A=!A:"=µ! FOR /F "tokens=1,2 delims=µ=" %%A IN ("!A!") DO ( SET first=%%A SET second=%%B echo !first! FOR /F "tokens=1,2 delims= " %%A IN ("!first!") DO ( echo %%A echo %%B ) echo !second! ) endlocal exit /b
这解析了该行的一个标记,我现在将使用它.
但我相信下面的代码就是你要找的.
因为文件夹的数量不同,我交换了输出中列的顺序.我首先放置浏览器描述,然后是文件夹,每列一个.这允许修复每列的定义.
我用jeb的答案中的信息包括“作为FOR分隔符.
编辑 – 我简化了代码
注 – 首次尝试使用的是使用Internet Explorer检索的XML副本.我发现IE改变了文件的格式.此代码高度依赖于文件的确切格式,因此它不适用于原始XML.它还可以作为解析为什么批处理是解析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! ) ) ) )
如果代码将失败!出现在任何描述中,因为延迟扩展会破坏包含!的任何FOR变量的扩展.我查了一下,你的文件不包含!在任何描述中.
可以修改代码来处理!在描述中,但它会变得更复杂.它需要打开和关闭延迟扩展,并在ENDLOCAL屏障上保留变量值.
上面的代码高度依赖于XML的格式.如果删除非标准破折号,或者空格排列发生变化,则会失败.
以下变体更加健壮,但仍需要每行包含一个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 ( 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! ) ) ) )
编辑 – 最后一个版本
这是一个可以处理的版本!在数据中.我在输出中添加了一个额外的列.第一列仍然是浏览器描述.第二列是useragent字符串.其余列是文件夹.该解决方案使用延迟扩展切换技术.它还使用额外的FOR / F来保留ENDLOCAL屏障的变量值.
@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 ) ) )