使用Windows批处理文件更改XML文件中的标记数据

时间:2022-11-27 17:15:22

I have an XML file with several tags in it, and I am trying to change the contents of one of these tags with a batch script.

我有一个带有多个标签的XML文件,我试图用批处理脚本更改其中一个标签的内容。

An example of the xml file is:

xml文件的一个示例是:

<ConfigurationData>
<ReportsFolder>\Reports</ReportsFolder>
<Helpfolder>\Help</HelpFolder>
<InstallationType>Terminal</InstallationType>
<LicenseFolder>..\License</LicenseFolder>
</ConfigurationData>

I am trying to change the data between <InstallationType> and </InstallationType> to another variable of my choosing. I'm thinking I would need some sort of For loop to find that part of the xml file, but I'm lost as to how I would edit just that one section.

我正在尝试将 和 之间的数据更改为我选择的另一个变量。我想我需要某种For循环来找到xml文件的那一部分,但是我对于如何编辑那一部分感到很遗憾。

3 个解决方案

#1


12  

You really should use a tool that is designed specifically to manipulate XML.

您真的应该使用专门设计用于操作XML的工具。

But in a pinch, you could use any tool that can do a regex search and replace to do a naive solution that would work with the file as you have it laid out, but might fail with a logically equivalent XML file that has had the physical layout rearranged.

但是,在紧要关头,您可以使用任何可以进行正则表达式搜索和替换的工具来执行一个天真的解决方案,该解决方案可以在文件布局时使用该文件,但可能会失败,因为逻辑上等效的XML文件具有物理布局重新排列。

I like to use a hybrid JScript/batch utility I wrote called REPL.BAT to manipulate text files via batch scripts. The script will run on any native Windows machine from XP onward, and it does not require installation of any 3rd party executables. Click the link to get the script code and a more thorough description.

我喜欢使用我编写的名为REPL.BAT的混合JScript /批处理实用程序来通过批处理脚本来处理文本文件。该脚本将从XP开始在任何本机Windows机器上运行,并且不需要安装任何第三方可执行文件。单击链接以获取脚本代码和更全面的描述。

Using REPL.BAT, a fast and efficient but naive solution is as simple as:

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

#2


8  

@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

Output with your example data:

使用示例数据输出:

<ConfigurationData>
<ReportsFolder>\Reports</ReportsFolder>
<Helpfolder>\Help</HelpFolder>
<InstallationType>New value</InstallationType>
<LicenseFolder>..\License</LicenseFolder>
</ConfigurationData>

#3


0  

GNU sed

sed "/InstallationType/s/>[^<]*</>New Value</" file.xml

..output:

<ConfigurationData>
<ReportsFolder>\Reports</ReportsFolder>
<Helpfolder>\Help</HelpFolder>
<InstallationType>New Value</InstallationType>
<LicenseFolder>..\License</LicenseFolder>
</ConfigurationData>

#1


12  

You really should use a tool that is designed specifically to manipulate XML.

您真的应该使用专门设计用于操作XML的工具。

But in a pinch, you could use any tool that can do a regex search and replace to do a naive solution that would work with the file as you have it laid out, but might fail with a logically equivalent XML file that has had the physical layout rearranged.

但是,在紧要关头,您可以使用任何可以进行正则表达式搜索和替换的工具来执行一个天真的解决方案,该解决方案可以在文件布局时使用该文件,但可能会失败,因为逻辑上等效的XML文件具有物理布局重新排列。

I like to use a hybrid JScript/batch utility I wrote called REPL.BAT to manipulate text files via batch scripts. The script will run on any native Windows machine from XP onward, and it does not require installation of any 3rd party executables. Click the link to get the script code and a more thorough description.

我喜欢使用我编写的名为REPL.BAT的混合JScript /批处理实用程序来通过批处理脚本来处理文本文件。该脚本将从XP开始在任何本机Windows机器上运行,并且不需要安装任何第三方可执行文件。单击链接以获取脚本代码和更全面的描述。

Using REPL.BAT, a fast and efficient but naive solution is as simple as:

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

#2


8  

@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

Output with your example data:

使用示例数据输出:

<ConfigurationData>
<ReportsFolder>\Reports</ReportsFolder>
<Helpfolder>\Help</HelpFolder>
<InstallationType>New value</InstallationType>
<LicenseFolder>..\License</LicenseFolder>
</ConfigurationData>

#3


0  

GNU sed

sed "/InstallationType/s/>[^<]*</>New Value</" file.xml

..output:

<ConfigurationData>
<ReportsFolder>\Reports</ReportsFolder>
<Helpfolder>\Help</HelpFolder>
<InstallationType>New Value</InstallationType>
<LicenseFolder>..\License</LicenseFolder>
</ConfigurationData>