如何使用Inno Setup循环更新多个XML节点? [英] How to update multiple XML nodes in a loop with Inno Setup?

查看:222
本文介绍了如何使用Inno Setup循环更新多个XML节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须使用Inno Setup更新出现多次的XML节点.

I have to update a XML node that appears more than once, using Inno Setup.

该怎么做?

例如:我必须更新<details>个节点(虽然我不知道有多少个节点)

For instance: I have to update <details> nodes (while I don't know how many nodes there are)

<server name="A">
    <details>id=5 gid=10</details>
</server>

<server name="B">
    <details>id=5 gid=10</details>
</server>

谢谢

推荐答案

这是@TLama修改后的代码版本,来自对

This is modified version of code by @TLama from an answer to How to read and write XML document node values?

除了他的代码外,该版本还可以更新与XPath匹配的多个节点.唯一的区别是对selectNodes而不是selectSingleNode的调用以及随后的for循环.

In addition to his code, this version can update multiple nodes matching the XPath. The only difference is the call to selectNodes instead of selectSingleNode and the following for loop.

procedure SaveValueToXMLNodes(const AFileName, APath, AValue: string);
var
  XMLDocument: Variant;
  XMLNodeList: Variant;
  Index: Integer;
begin
  XMLDocument := CreateOleObject('Msxml2.DOMDocument.6.0');
  try
    XMLDocument.async := False;
    XMLDocument.load(AFileName);
    if XMLDocument.parseError.errorCode <> 0 then
    begin
      MsgBox('The XML file could not be parsed. ' +
        XMLDocument.parseError.reason, mbError, MB_OK)
    end
      else
    begin
      XMLDocument.setProperty('SelectionLanguage', 'XPath');
      XMLNodeList := XMLDocument.selectNodes(APath);
      for Index := 0 to XMLNodeList.length - 1 do
      begin
        XMLNodeList.item[Index].text := AValue;
      end;
      XMLDocument.save(AFileName);
    end;
  except
    MsgBox('An error occured!' + #13#10 + GetExceptionMessage, mbError, MB_OK);
  end;
end;

对于输入文件,例如:

<root>
    <server name="A">
        <details>id=5 gid=10</details>
    </server>
    <server name="B">
        <details>id=5 gid=10</details>
    </server>
</root>

您可以使用如下代码:

SaveValueToXMLNodes('servers.xml', '/root/server/details', 'id=6 gid=11');

获得:

<root>
    <server name="A">
        <details>id=6 gid=11</details>
    </server>
    <server name="B">
        <details>id=6 gid=11</details>
    </server>
</root>

这篇关于如何使用Inno Setup循环更新多个XML节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆