我想读取XML文件的一些节点,并在一些自定义输入字段中显示它们的值。用户可以根据需要更改这些值,并通过单击Next
按钮将这些值保存回XML。如何读取和写入XML文档节点值?
如何在InnoSetup脚本中做到这一点?
我想读取XML文件的一些节点,并在一些自定义输入字段中显示它们的值。用户可以根据需要更改这些值,并通过单击Next
按钮将这些值保存回XML。如何读取和写入XML文档节点值?
如何在InnoSetup脚本中做到这一点?
使用CreateOleObject
函数实例化标准的MSXML2.DOMDocument
COM对象。下面的脚本显示了如何加载和保存文本值用于从下面贴(脚本本身通过从MSDN实施例的启发)的XML文件中的单个节点:
[Setup]
AppName=My Program
AppVersion=1.5
DefaultDirName={pf}\My Program
DefaultGroupName=My Program
UninstallDisplayIcon={app}\MyProg.exe
Compression=lzma2
SolidCompression=yes
OutputDir=userdocs:Inno Setup Examples Output
[Files]
Source: "MyProg.exe"; DestDir: "{app}"
Source: "MyProg.chm"; DestDir: "{app}"
[Icons]
Name: "{group}\My Program"; Filename: "{app}\MyProg.exe"
[Code]
var
CustomEdit: TEdit;
CustomPageID: Integer;
function LoadValueFromXML(const AFileName, APath: string): string;
var
XMLNode: Variant;
XMLDocument: Variant;
begin
Result := '';
XMLDocument := CreateOleObject('Msxml2.DOMDocument.6.0');
try
XMLDocument.async := False;
XMLDocument.load(AFileName);
if (XMLDocument.parseError.errorCode <> 0) then
MsgBox('The XML file could not be parsed. ' +
XMLDocument.parseError.reason, mbError, MB_OK)
else
begin
XMLDocument.setProperty('SelectionLanguage', 'XPath');
XMLNode := XMLDocument.selectSingleNode(APath);
Result := XMLNode.text;
end;
except
MsgBox('An error occured!' + #13#10 + GetExceptionMessage, mbError, MB_OK);
end;
end;
procedure SaveValueToXML(const AFileName, APath, AValue: string);
var
XMLNode: Variant;
XMLDocument: Variant;
begin
XMLDocument := CreateOleObject('Msxml2.DOMDocument.6.0');
try
XMLDocument.async := False;
XMLDocument.load(AFileName);
if (XMLDocument.parseError.errorCode <> 0) then
MsgBox('The XML file could not be parsed. ' +
XMLDocument.parseError.reason, mbError, MB_OK)
else
begin
XMLDocument.setProperty('SelectionLanguage', 'XPath');
XMLNode := XMLDocument.selectSingleNode(APath);
XMLNode.text := AValue;
XMLDocument.save(AFileName);
end;
except
MsgBox('An error occured!' + #13#10 + GetExceptionMessage, mbError, MB_OK);
end;
end;
procedure InitializeWizard;
var
CustomPage: TWizardPage;
begin
CustomPage := CreateCustomPage(wpWelcome, 'Custom Page',
'Enter the new value that will be saved into the XML file');
CustomPageID := CustomPage.ID;
CustomEdit := TEdit.Create(WizardForm);
CustomEdit.Parent := CustomPage.Surface;
end;
procedure CurPageChanged(CurPageID: Integer);
begin
if CurPageID = CustomPageID then
CustomEdit.Text := LoadValueFromXML('C:\Setup.xml', '//Setup/FirstNode');
end;
function NextButtonClick(CurPageID: Integer): Boolean;
begin
Result := True;
if CurPageID = CustomPageID then
SaveValueToXML('C:\Setup.xml', '//Setup/FirstNode', CustomEdit.Text);
end;
下面是在所使用的XML文件脚本:
<?xml version="1.0" encoding="UTF-8"?>
<Setup>
<FirstNode>First node value!</FirstNode>
<SecondNode>Second node value!</SecondNode>
</Setup>
P.S.将这个脚本中的每个OLE对象函数调用与['OleCheck'](http://www.jrsoftware.org/ishelp/topic_isxfunc_olecheck.htm)包装在一起会很好,这会在函数调用时引发异常失败(当结果与“S_OK”值不同时)。 – TLama
另请参阅示例[CodeAutomation.iss](https://woofy.googlecode.com/hg/tools/Inno%20Setup/Examples/CodeAutomation.iss)。 – Deanna
@Deanna,在我发布这个之前,我正在研究这个例子,但它是关于如何将节点追加到XML文件中,同时这是关于如何加载和保存现有节点值;-) – TLama
相关问题:[Inno Setup基于自定义输入修改XML文件](http://stackoverflow.com/q/8141886/588306)。 – Deanna