【问题标题】:How to read multiple XML nodes? (Inno Setup)如何读取多个 XML 节点? (创新设置)
【发布时间】:2017-05-10 18:56:10
【问题描述】:

这是我要阅读的 XML。我有同名的节点。我想访问节点以在组合框中显示国家/地区并在列表框中显示货币。

这就是 XML 的样子:

<listaPaises>
  <item>
     <id>1</id>
     <name>MÉXICO</name>
     <suggestedCurrency>PESO MEXICANO</suggestedCurrency>
  </item>
  <item>
     <id>4</id>
     <name>ARGENTINA</name>
     <suggestedCurrency>PESO ARGENTINO</suggestedCurrency>
  </item>
  <item>
     <id>23</id>
     <name>BELICE</name>
     <suggestedCurrency>DÓLAR BELICEÑO</suggestedCurrency>
  </item>
  <item>
     <id>5</id>
     <name>BOLIVIA</name>
     <suggestedCurrency>BOLIVIANO</suggestedCurrency>
  </item>
</listaPaises>

这就是我想要的:

【问题讨论】:

  • 使用CreateOleObject 函数实例化标准MSXML2.DOMDocument 逐个读取节点并添加到组合等(没有图标)它们根本不在xml中。 how-to-read-xml-document-node-values
  • 让我知道你走了多远 :)

标签: xml inno-setup pascalscript


【解决方案1】:

使用标准的MSXML2.DOMDocument COM 对象及其SelectNodes 方法。

function LoadValuesFromXML(FileName: string): Boolean;
var
  XMLNode: Variant;
  XMLNodeList: Variant;
  XMLDocument: Variant;  
  Index: Integer;
begin
  XMLDocument := CreateOleObject('Msxml2.DOMDocument.6.0');
  try
    XMLDocument.async := False;
    XMLDocument.load(FileName);
    if (XMLDocument.parseError.errorCode <> 0) then
    begin
      Log('The XML file could not be parsed. ' + XMLDocument.parseError.reason);
      Result := False;
    end
      else
    begin
      XMLDocument.setProperty('SelectionLanguage', 'XPath');
      XMLNodeList := XMLDocument.SelectNodes('/listaPaises/item');
      for Index := 0 to XMLNodeList.length - 1 do
      begin
        XMLNode := XMLNodeList.item[Index];
        Log(
          Format('Name = %s; Currency = %s', [
            XMLNode.SelectSingleNode('name').Text,
            XMLNode.SelectSingleNode('suggestedCurrency').Text])); 
      end;
      Result := True;
    end;
  except
    Log('An error occured!' + #13#10 + GetExceptionMessage);
    Result := False;
  end;
end;

使用您的 XML 文件,它将记录:

Name = MÉXICO; Currency = PESO MEXICANO       
Name = ARGENTINA; Currency = PESO ARGENTINO   
Name = BELICE; Currency = DÓLAR BELICEÑO      
Name = BOLIVIA; Currency = BOLIVIANO          

只需使用该信息来填充您的组合框和列表框(如果您不知道如何操作,这是一个单独的问题)。


以上基于How to update multiple XML nodes in a loop with Inno Setup?

【讨论】:

  • 非常感谢,它有效,我只在'//listaPaises/item'中添加一个“/”。
  • 只有在&lt;listaPaises&gt; 不是根元素时才需要。
猜你喜欢
  • 2017-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-18
  • 2020-12-18
  • 1970-01-01
相关资源
最近更新 更多