Change tag data in an XML file using a Windows batch file

I have an XML file with several tags in it, and I'm trying to modify the contents of one of these tags using a script package.

Example xml file:

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

I am trying to change data between <InstallationType> and </InstallationType> another variable of my choice. I think I will need some For loop to find this part of the xml file, but I lost the way I would edit only one section.

+3
windows xml batch-file
Jun 11 '13 at 21:38
source share
3 answers

You really need to use a tool specifically designed for XML management.

But as a last resort, you can use any tool that can search for and replace regular expressions to make a naive decision that will work with the file as you have outlined it, but it may fail with a logically equivalent XML file that has been remade physical layout.

I like to use the hybrid JScript / batch utility that I wrote under the name REPL.BAT to manage text files using batch scripts. The script will run on any native Windows machine with XP, and does not require the installation of any third-party executable files. Click the link to get the script code and a more detailed description.

Using REPL.BAT, a quick and effective but naive solution is as simple as:

 setlocal enableDelayedExpansion set "newValue=myNewValue" type "fileName.xml"|repl "(<InstallationType>).*(</InstallationType>)" "$1!newValue!$2" >fileName.xml.new move /y "fileName.xml.new" "fileName.xml" 
+10
Jun 12 '13 at 4:44
source share
 @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 example data:

 <ConfigurationData> <ReportsFolder>\Reports</ReportsFolder> <Helpfolder>\Help</HelpFolder> <InstallationType>New value</InstallationType> <LicenseFolder>..\License</LicenseFolder> </ConfigurationData> 
+8
Jun 11 '13 at 22:17
source share

GNU sed

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

.. exit:

 <ConfigurationData> <ReportsFolder>\Reports</ReportsFolder> <Helpfolder>\Help</HelpFolder> <InstallationType>New Value</InstallationType> <LicenseFolder>..\License</LicenseFolder> </ConfigurationData> 
0
Jun 11 '13 at 22:26
source share



All Articles