如何读写XML文档节点值?

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

在InnoSetup脚本中怎么做?

使用 CreateOleObject功能实例化标准的 MSXML2.DOMDocument COM对象.以下脚本显示如何从下面发布的XML文件中加载并保存单个节点的文本值(脚本本身受MSDN的示例启发):
[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,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,'XPath');
      XMLNode := XMLDocument.selectSingleNode(APath);
      XMLNode.text := AValue;
      XMLDocument.save(AFileName);
    end;
  except
    MsgBox('An error occured!' + #13#10 + GetExceptionMessage,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>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念
xml文件介绍及使用
xml编程(一)-xml语法
XML文件结构和基本语法
第2章 包装类
XML入门的常见问题(二)
Java对象的强、软、弱和虚引用
JS解析XML文件和XML字符串详解
java中枚举的详细使用介绍
了解Xml格式
XML入门的常见问题(四)
深入SQLite多线程的使用总结详解
PlayFramework完整实现一个APP(一)
XML和YAML的使用方法
XML轻松学习总节篇