我有一个像这样的XML文件:
<fruit>
<desc>xxx / yyy / zzz</desc>
</fruit>
<fruit>
<desc></desc>
</fruit>
<fruit>
<desc>abc / def / ghi</desc>
</fruit>
我使用批处理脚本来修复节点&#34; desc&#34;。 这是修复后的结果:
<fruit>
<desc>xxx/yyy/zzz</desc>
</fruit>
<fruit>
<desc>N/A</desc>
</fruit>
<fruit>
<desc>abc/def/ghi</desc>
</fruit>
如何在不扫描所有文件两次的情况下更换2个不同的字符串? 这是脚本:
@echo off
setlocal enabledelayedexpansion
set "input_xml=xmlfile.xml"
set "search=^<desc^>^</desc^>"
set "search2= / "
set "replace=^<desc^>N/A^</desc^>"
set "replace2=/"
for /f "delims=" %%i in ('type "!input_xml!" ^& break ^> "!input_xml!" ') do (
set FixNullNode=%%i
echo !FixNullNode:%search%=%replace%! >>"!input_xml!"
)
for /f "delims=" %%v in ('type "!input_xml!" ^& break ^> "!input_xml!" ') do (
set FixSpaceSlash=%%v
echo !FixSpaceSlash:%search2%=%replace2%! >>"!input_xml!"
)
pause
由于
答案 0 :(得分:0)
将第二个替换移动到第一个命令。
@echo off
setlocal enabledelayedexpansion
set "input_xml=xmlfile.xml"
set "search=^<desc^>^</desc^>"
set "search2= / "
set "replace=^<desc^>N/A^</desc^>"
set "replace2=/"
for /f "delims=" %%i in ('type "!input_xml!" ^& break ^> "!input_xml!" ') do (
set "Fix=%%i"
set "Fix=!Fix:%search%=%replace%!"
echo !Fix:%search2%=%replace2%! >>"!input_xml!"
)