如何读取多个XML节点? (创新设置) [英] How to read multiple XML nodes? (Inno Setup)

查看:93
本文介绍了如何读取多个XML节点? (创新设置)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

This is the XML that I want to read. I have nodes with the same name. I want to access the nodes to show countries on a combo box and currencies on a list box.

这是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>

这就是我想要的:

推荐答案

使用标准的 SelectNodes 方法.

Use the standard MSXML2.DOMDocument COM object and its SelectNodes method.

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文件,它将记录:

With your XML file, it will log:

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

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

Just use that information to populate your combo box and list box (what is a separate question, if you do not know how).

以上内容基于如何使用Inno Setup循环更新多个XML节点?

这篇关于如何读取多个XML节点? (创新设置)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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