我有一個XML文件,應該通過爲非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^> ^<^!--"
的'!'
是否有逃避'!'
符號的任何特殊的方式?
delims =¶是假的一。更改爲delims = – 2013-02-26 06:47:03