DOS批量提取块来自xml文件

时间:2013-10-16 12:47:29

标签: xml batch-file

我找不到办法(尝试通过StackOverflow搜索)。

我有一个格式化的xml文件,我必须按原样从中提取一个块(不修改格式,选项卡,<,>等等)。例如,让我们说有一个名为some.xml

的文件

该块由以下分隔:

 <!- Let us call this tag_begin -->
 <Ask Ref_Ask="XXXYYYYY">   
   ...
 <!- Let us call this tag_end -->
 </Ask>  

我成功地为blockend标签提取开始和结束行,但是我不能将所有行长度放入result.txt文件中:行停在127个字符长度:出了什么问题?

@echo off

Set Tag_Begin="<Ask Ref_Ask="
Set Tag_End="</Ask>"
set NB=XXXYYYY
set /A CPTE=0
set Line_Begin=
set Line_End=


Rem --- Find all possible start of block lines, and extract the good one set to Line_Begin
findstr /n /C:%Tag_Begin% some.xml | find /i "%NB%"> temporary.txt
for /f "tokens=1 delims=:" %%L in (temporary.txt) do set Line_Begin=%%L

Rem --- Finding the line order (from all possible start lines) which match the good one
findstr /n /C:%Tag_Begin% some.xml | findstr /n "%NB%"> temporary.txt
for /f "tokens=1 delims=:" %%O in (temporary.txt) do set order_begin=%%O
set /A order_begin-=1


Rem -- Looking for all possible end of block lines, and extract the "order_begin" one for Line_End
for /f "skip=%order_begin% tokens=1 delims=:" %%F in ('findstr /n /C:%Tag_End% some.xml') do set Line_End=%%F & goto away
:away


setlocal enabledelayedexpansion
for /f %%L in (some.xml) do (
set /A CPTE+=1
echo CPTE=!CPTE!
if !CPTE! GEQ %Line_Begin% if !CPTE! LEQ %Line_End% echo %%L >> result.txt
)
setlocal disabledelayedexpansion

del temporary.txt

2 个答案:

答案 0 :(得分:0)

未经测试:

        @echo off
        for /f "tokens=1 delims=:" %%L in ('findstr /n "<!- Let us call this begin tag -->" ssome.xml') do ( 
         set begin_line=%%L
        )

        for /f "tokens=1 delims=:" %%L in ('findstr /n "<!- Let us call this end tag -->" some.xml') do ( 
         set /a end_line=%%L+1
        )

        echo showing lines between %end_line% and %begin_line%
        break>"%temp%\empty"
        fc "%temp%\empty" "some.xml" /lb  %end_line% /t |more +4 | findstr /B /E /V "*****" | more +%begin_line%
        del /Q /F "%temp%\empty"

使用您的文件更改三个地方的some.xml

答案 1 :(得分:0)

这使用了来自 - http://www.dostips.com/forum/viewtopic.php?f=3&t=4697

的名为findrepl.bat的帮助程序批处理文件

findrepl.bat放在与批处理文件相同的文件夹中。

@echo off
type some.xml|findrepl "<Ask Ref_Ask=.XXXYYYYY.>" /e:"</Ask>" >newfile.xml