如何读取多个XML节点? (Inno设置)

时间:2017-05-10 18:56:10

标签: xml inno-setup pascalscript

这是我想要阅读的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>

这就是我想要的:Image

1 个答案:

答案 0 :(得分: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?