XML字符串替换在Windows批处理文件 [英] XML string replacement in Windows batch file

查看:105
本文介绍了XML字符串替换在Windows批处理文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有页眉和页脚像一个XML文件:

 设置标题=< XML版本=1.0编码=UTF-8><配置的xmlns =http://namespace.com/config XMLNS:XSI =http://www.w3.org/2001/XMLSchema-instance版本=2.0>中
设置页脚=< /配置>中

我想从XML文件然后在其余项目中的页眉和页脚标签之间读取删除它们。

我试图用sed。这个工作在Linux,但不能在Windows做任何事情。

  SED -eS @%标题%@@ G-i.backup xmlFile.xml什么建议?


解决方案

在Windows中,您必须使用反斜线双引号:

  @ECHO OFF&安培; SETLOCAL
设置?XMLheader =&下; XML版本= \\1.0 \\编码= \\UTF-8 \\>&下;配置的xmlns = \\?的http://namespace.com/config \\的xmlns:的xsi = \\ HTTP://www.w3.org/2001/XMLSchema-instance \\版= \\2.0 \\>中
设置页脚=< /配置>中
SEDS @%xmlHeader%\\ |%躯%@@ G文件

在命令行上示例:

 >文件类型
    ?< XML版本=1.0编码=UTF-8><配置的xmlns =htt​​p://namespace.com/config的xmlns:XSI =htt​​p://www.w3.org/ 2001 / XML模式实例版本=2.0>
    <&TAG1 GT; INFO1变化在环路LT; /标记1>
    <&TAG2 GT; INFO2变化在环路LT; / TAG2>
    < /配置>    >的sed?s @< XML版本= \\1.0 \\编码= \\UTF-8 \\><配置的xmlns = \\?HTTP://namespace.com/config \\的xmlns:XSI = \\HTTP://www.w3.org/2001/XMLSchema-instance \\版= \\2.0 \\> \\ |< /配置> @@ G文件    <&TAG1 GT; INFO1变化在环路LT; /标记1>
    <&TAG2 GT; INFO2变化在环路LT; / TAG2>

注: SED 取值命令先按g 标志$ C>在此没有必要。

I have a xml file with header and footer like:

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>"

I want to remove them from the xml file then read in the remaining items in between the header and footer tags.

I've tried to use sed. this works on Linux but don't do anything on Windows

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

解决方案

In Windows you must escape double quotes with a backslash:

@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

Example on the command line:

    >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>

Note: the g flag for the s command of sed is not necessary here.

这篇关于XML字符串替换在Windows批处理文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆