use*_*144 4 xml windows batch-file
我有一个包含多个标签的XML文件,我试图用批处理脚本更改其中一个标签的内容.
xml文件的一个示例是:
<ConfigurationData>
<ReportsFolder>\Reports</ReportsFolder>
<Helpfolder>\Help</HelpFolder>
<InstallationType>Terminal</InstallationType>
<LicenseFolder>..\License</LicenseFolder>
</ConfigurationData>
Run Code Online (Sandbox Code Playgroud)
我试图改变之间的数据<InstallationType>和</InstallationType>我的选择的另一个变量.我想我需要某种For循环来找到xml文件的那一部分,但是我对如何编辑那一部分感到很遗憾.
dbe*_*ham 12
您真的应该使用专门设计用于操作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"
Run Code Online (Sandbox Code Playgroud)
@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
Run Code Online (Sandbox Code Playgroud)
使用示例数据输出:
<ConfigurationData>
<ReportsFolder>\Reports</ReportsFolder>
<Helpfolder>\Help</HelpFolder>
<InstallationType>New value</InstallationType>
<LicenseFolder>..\License</LicenseFolder>
</ConfigurationData>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
40129 次 |
| 最近记录: |