Inno setup: how to replace a string in XML file? [

2019-04-29 03:41发布

问题:

This question already has an answer here:

  • Inno Setup - How to save a node in a specific line 1 answer

The following (in quote) is the content of a XML file which is part of my package. I'd like to replace the value of c:\path\myapp.exe during the installation (with the real path where the user chose to install the application. Is that possible? How to?

<?xml version="1.0" encoding="UTF-8"?>
<launchConfiguration type="org.eclipse.ui.externaltools.ProgramLaunchConfigurationType">
   <listAttribute key="org.eclipse.debug.ui.favoriteGroups">
      <listEntry value="org.eclipse.ui.externaltools.launchGroup"/>
   </listAttribute>
   <stringAttribute key="org.eclipse.ui.externaltools.ATTR_LAUNCH_CONFIGURATION_BUILD_SCOPE" value="${none}"/>
   <stringAttribute key="org.eclipse.ui.externaltools.ATTR_LOCATION" value="c:\path\myapp.exe"/>
   <stringAttribute key="org.eclipse.ui.externaltools.ATTR_TOOL_ARGUMENTS" value="${resource_loc}"/>
</launchConfiguration>

回答1:

Your best option is to use the XML DOM to select and edit the required node as TLama suggested.

Alternatively, you can install a template file with a known string in the location you want to replace. The file can then be read as a string, modified and written back out again using something like:

[Code]
procedure WriteAppPath;
var
    FileData: String;
begin
    LoadStringFromFile(ExpandConstant('{app}\app.xml'), FileData);
    StringChange(FileData, 'XXXXXMARKERXXXXX', ExpandConstant('{app}'));
    SaveStringToFile(ExpandConstant('{app}\app.xml'), FileData, False);
end;

See also this question about doing the same thing en masse to an INI file.