格式化 XML 文件的批处理脚本 |搜索字符串并将其注释掉 [英] Batch-Script for formatting XML-files | Search for Strings and comment them out
问题描述
所以我有一个未格式化的 XML 文件,这意味着它只有一行.我遇到的问题是,我想删除两行或将它们注释掉.
So I have a XML-File, which isn't formatted, meaning it only has one line. The problem I have is, I want to delete two lines or comment them out.
有没有办法使用批处理脚本来格式化 XML 文件,所以如果我有:
Is there a way to format the XML-file using a batch-script, so if i have:
<xml><tag><othertag></othertag></tag></xml>
输出将是
<xml>
<tag>
<othertag>
</othertag>
</tag>
</xml>
或者,当我只有一个模式并且不能使用 findstr/v,因为我只有一行时,我该如何替换/添加字符串?
Alternatively, how can I replace/add Strings, when I only have a pattern, and can't use findstr /v, because I only have one line?
提前致谢!
推荐答案
@echo off
setlocal EnableDelayedExpansion
rem Create a variable with spaces
set "spaces= "
for /L %%i in (1,1,5) do set "spaces=!spaces!!spaces!"
rem Read the line
for /F "delims=" %%a in (input.txt) do set "line=%%a"
set level=0
:nextTag
rem Separate first tag from line
for /F "tokens=1* delims=<" %%a in ("!line!") do (
set "tag=%%a"
set "line=%%b"
)
rem Show first tag in one separate line
if "%tag:~0,1%" equ "/" set /A level-=5
echo !spaces:~0,%level%!^<!tag!
if "%tag:~0,1%" neq "/" set /A level+=5
if defined line goto nextTag
输出:
<xml>
<tag>
<othertag>
</othertag>
</tag>
</xml>
请注意,以这种方式可以处理的行的最大长度约为 8150 个字符.此外,此解决方案从文件中删除所有感叹号.如果需要,这两个点都可以固定.
Note that the maximum length of a line that can be processed this way is 8150 characters approx. Also, this solution remove any exclamation-marks from the file. Both points can be fixed, if required.
这篇关于格式化 XML 文件的批处理脚本 |搜索字符串并将其注释掉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!