1
votes

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.

This how XML looks like:

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

This is what I want: Image

1
Use the CreateOleObject function to instantiate the standard MSXML2.DOMDocument read the nodes one by one and add to the combo etc. (without icons) They are simply not in the xml. how-to-read-xml-document-node-values - moskito-x
Let me know how far you come :) - moskito-x

1 Answers

1
votes

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;

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).


The above is based on How to update multiple XML nodes in a loop with Inno Setup?