【发布时间】:2013-02-27 22:43:34
【问题描述】:
我有一个 XML 文件,应该通过为非 XML 标记中的项目添加 cmets 来很好地格式化该文件。示例输入文件如下所示。
comment 1
<book id=1>
Book 1
</book>
comment 2
<book id=2>
Book 2
</book>
comment 3
<book id=3>
Book 3
</book>
预期输出
<!-- comment 1 -->
<book id=1>
Book 1
</book>
<!-- comment 2 -->
<book id=2>
Book 2
</book>
<!-- comment 3 -->
<book id=3>
Book 3
</book>
编写的批处理脚本。
@ECHO off
SETLOCAL enabledelayedexpansion
SET INTEXTFILE=test.xml
SET OUTTEXTFILE=out.xml
SET "SEARCH_TEXT_1=^<book "
SET "REPLACE_TEXT_1=--^> ^<book "
SET "SEARCH_TEXT_2=^</book^>"
SET "REPLACE_TEXT_2=^</book^> ^<^!--"
SET "comment=<^!--- Converted to well formed XML --> <^!--"
ECHO !comment! > %OUTTEXTFILE%
for /f "tokens=1,* delims=¶" %%A in ( '"type %INTEXTFILE%"') do (
SET string=%%A
SET modified=!string:%SEARCH_TEXT_1%=%REPLACE_TEXT_1%!
SET modified=!modified:%SEARCH_TEXT_2%=%REPLACE_TEXT_2%!
ECHO !modified! >> %OUTTEXTFILE%
)
错误:
< was unexpected at this time.
这是由于SET "REPLACE_TEXT_2=^</book^> ^<^!--" 行中的'!' 导致'!' 符号转义有什么特殊的方法吗?
【问题讨论】:
标签: windows batch-file