我有一个XML文件中有几个标签,我正在尝试用批处理脚本来更改其中一个标签的内容。
xml文件的一个例子是:
<ConfigurationData> <ReportsFolder>\Reports</ReportsFolder> <Helpfolder>\Help</HelpFolder> <InstallationType>Terminal</InstallationType> <LicenseFolder>..\License</LicenseFolder> </ConfigurationData>
我试图改变<InstallationType>
和</InstallationType>
之间的数据到我select的另一个variables。 我想我会需要某种For循环findXML文件的一部分,但我失去了如何编辑只是一个部分。
你真的应该使用专门设计来处理XML的工具。
但是,在一个捏,你可以使用任何工具,可以做一个正则表达式搜索和替换做一个天真的解决方案,可以使用该文件,因为它已经布置,但可能会失败,一个逻辑上等效的XML文件,有物理布局重新排列。
我喜欢使用我写的名为REPL.BAT的混合JScript /批处理实用程序来通过批处理脚本处理文本文件。 该脚本将在XP以后的任何本地Windows机器上运行,并且不需要安装任何第三方可执行文件。 点击链接获取脚本代码和更全面的描述。
使用REPL.BAT,一个快速而高效但天真的解决方案就像下面这样简单:
setlocal enableDelayedExpansion set "newValue=myNewValue" type "fileName.xml"|repl "(<InstallationType>).*(</InstallationType>)" "$1!newValue!$2" >fileName.xml.new move /y "fileName.xml.new" "fileName.xml"
@echo off setlocal EnableDelayedExpansion set anotherVariable=New value (for /F "delims=" %%a in (theFile.xml) do ( set "line=%%a" set "newLine=!line:InstallationType>=!" if "!newLine!" neq "!line!" ( set "newLine=<InstallationType>%anotherVariable%</InstallationType>" ) echo !newLine! )) > newFile.xml
用您的示例数据输出:
<ConfigurationData> <ReportsFolder>\Reports</ReportsFolder> <Helpfolder>\Help</HelpFolder> <InstallationType>New value</InstallationType> <LicenseFolder>..\License</LicenseFolder> </ConfigurationData>
sed "/InstallationType/s/>[^<]*</>New Value</" file.xml
..output:
<ConfigurationData> <ReportsFolder>\Reports</ReportsFolder> <Helpfolder>\Help</HelpFolder> <InstallationType>New Value</InstallationType> <LicenseFolder>..\License</LicenseFolder> </ConfigurationData>