我有一个 XML 文件,应该通过为非 XML 标记中的项目添加注释来很好地格式化该文件。示例输入文件如下所示。
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^> ^<^!--"
是否有任何特殊的方式来转义'!'
符号?