gpt4 book ai didi

xml - 如何在 Inno Setup 中读写 XML 文档节点值?

转载 作者:数据小太阳 更新时间:2023-10-29 01:40:50 25 4
gpt4 key购买 nike

我想读取 XML 文件的一些节点并在一些自定义输入字段中显示它们的值。然后,用户可以根据需要更改这些值,并通过单击 Next 按钮将这些值保存回 XML。

如何在 Inno Setup 脚本中执行此操作?

最佳答案

使用 CreateOleObject实例化标准的函数MSXML2.DOMDocument对象。以下脚本显示了如何从下面发布的 XML 文件加载和保存单个节点的文本值(脚本本身的灵感来自 MSDN 中的示例):

[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>

关于xml - 如何在 Inno Setup 中读写 XML 文档节点值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11250266/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com