Windows批处理文件中的XML字符串替换

时间:2013-07-30 16:13:58

标签: batch-file

我有一个带有页眉和页脚的xml文件,如:

set "header=<?xml version="1.0" encoding="UTF-8"?><Config   xmlns="http://namespace.com/config" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0">"
set "footer=</Config>"

我想从xml文件中删除它们,然后读入页眉和页脚标记之间的其余项目。

我试过用sed。这适用于Linux但不在Windows上执行任何操作

sed -e "s@%header%@@g" -i.backup xmlFile.xml any suggestions?

1 个答案:

答案 0 :(得分:3)

在Windows中,您必须使用反斜杠转义双引号:

@ECHO OFF &SETLOCAL
set "XMLheader=<?xml version=\"1.0\" encoding=\"UTF-8\"?><Config   xmlns=\"http://namespace.com/config\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" version=\"2.0\">"
set "footer=</Config>"
sed "s@%xmlHeader%\|%footer%@@g" file

命令行上的示例:

    >type file
    <?xml version="1.0" encoding="UTF-8"?><Config   xmlns="http://namespace.com/config" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0">
    <tag1>info1 changes in the loop</tag1>
    <tag2>info2 changes in the loop</tag2>
    </Config>

    >sed "s@<?xml version=\"1.0\" encoding=\"UTF-8\"?><Config   xmlns=\"http://namespace.com/config\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" version=\"2.0\">\|</Config>@@g" file

    <tag1>info1 changes in the loop</tag1>
    <tag2>info2 changes in the loop</tag2>

注意:此处不需要g s命令的sed标记。

相关问题