使用批处理脚本在XML中的特定行插入字符串

时间:2014-04-10 00:15:38

标签: xml string batch-file insert newline

我是编程新手。 some1可以告诉我如何使用批处理脚本在XML文件中插入新行吗? 当前文件有前两行:

<?xml version="1.0"?>
<Workbook xmlns="urn:schemas-microsoft-com:xxxxxxxxx

我想在第2行添加格式字符串,因此它具有:

<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:xxxxxxxxxx

它必须使用批处理文件,因为文件的其余部分是用它构建的。

干杯, 艾伦

2 个答案:

答案 0 :(得分:0)

我假设你在Windows上。如果您还没有Unix Utils,我建议您首先抓取它并将其添加到PATH

然后你可以做

 sed "1 a <?mso-application progid=\"Excel.Sheet\"?>" Input.xml > Output.xml
 move Output.xml Input.xml

请注意,sed的Windows版本可能不支持-i选项,但如果有,则可以将其缩短为

sed -i "1 a <?mso-application progid=\"Excel.Sheet\"?>" Input.xml

答案 1 :(得分:0)

@echo off
    setlocal enableextensions disabledelayedexpansion
    set "secondLine=^<?mso-application progid="Excel.Sheet"?^>"
    (for /f "delims=" %%a in (input.xml) do (
        echo(%%a
        if defined secondLine ( 
            echo(%secondLine%
            set "secondLine="
        )
    )) > output.xml