Sunil Sharma
Sunil Sharma

Reputation: 772

reading a xml file using Xpath in inno script

I have this inno script where i am trying to read a xml file and change the values by giving the possibility to user to change as custom inputs.

here is the code i am using :

function LoadValueFromXML(const AFileName, APath: string): string;
var
  XMLNode: Variant;
  XMLDocument: Variant;  
begin
  Result := '';
  XMLDocument := CreateOleObject('Msxml2.DOMDocument.6.0');
  try
    XMLDocument.async := False;
    XMLDocument.load(AFileName);
    if (XMLDocument.parseError.errorCode <> 0) then
      MsgBox('The XML file could not be parsed. ' + 
        XMLDocument.parseError.reason, mbError, MB_OK)
    else
    begin
      XMLDocument.setProperty('SelectionLanguage', 'XPath');
      XMLNode := XMLDocument.selectSingleNode(APath);
      Result := XMLNode.text;
    end;
  except
    MsgBox('An error occured!', mbError, MB_OK);
  end;
end;

this is the code for calling above function to load the values from XML

 EditMailServerID.Text := LoadValueFromXML('C:\dnpr\ERPBO\Settings.xml', '//settings/Mail/MailServer/');
   EditMailServerUserId.Text := LoadValueFromXML('C:\dnpr\ERPBO\Settings.xml', '//settings/Mail/UserName/');
   EditMailServerPassword.Text := LoadValueFromXML('C:\dnpr\ERPBO\Settings.xml', '//settings/Mail/Password/');
   EditERPBOServicePath.Text := LoadValueFromXML('C:\dnpr\ERPBO\Settings.xml', '//settings/default/ERPBOWebServicePath/');
   EditERPHOServicePath.Text := LoadValueFromXML('C:\dnpr\ERPBO\Settings.xml', '//settings/default/ERPHOWebServicePath/');
   EditCustomerId.Text := LoadValueFromXML('C:\dnpr\ERPBO\Settings.xml', '//settings/license/customernumber/');

sample xml code is:

<?xml version="1.0" encoding="utf-8"?>
<settings> 
  <default>
    <ReadInvoiceFile>0</ReadInvoiceFile>
    <HOUser>
    </HOUser>
    <HOShopNo>1</HOShopNo>
    <LagerShop>500</LagerShop>
    <SenderAddress>[email protected]</SenderAddress>
    <HostId>192.168.6.155</HostId>
    <PincodeDownloadFileName>tilbud5.txt</PincodeDownloadFileName>
    <PrinterName />
    <UseAverageCostPrice>true</UseAverageCostPrice>
    <ERPBOWebServicePath>http://localhost:90/FlisekompanietERPBOWebService/ERPBOService.asmx</ERPBOWebServicePath>
    <ERPHOWebServicePath>http://localhost:90/FlisekompanietERPHOWebService/ERPHOService.asmx</ERPHOWebServicePath>   
  </default>
  <license>
    <customernumber>998877</customernumber>
    <custid>532</custid>
  </license> 
  <Mail>
    <MailServer>dnserver.datanova.no</MailServer>
    <UserName>sharma</UserName>
    <Password>datanova123</Password>
  </Mail>
  <sms>
    <provider>test</provider>
    <sendername>test</sendername>
    <username>test</username>
    <password>test</password>
  </sms> 
</settings>

I get a runtime error at :

XMLDocument.setProperty('SelectionLanguage', 'XPath');
      XMLNode := XMLDocument.selectSingleNode(APath);

Upvotes: 4

Views: 1802

Answers (1)

TLama
TLama

Reputation: 76713

You have an extra / slash at the end of your XPath expressions when calling the function. Just remove them and you'll get it to work. Here is the fix:

EditMailServerID.Text := LoadValueFromXML('C:\dnpr\ERPBO\Settings.xml', 
  '//settings/Mail/MailServer');
EditMailServerUserId.Text := LoadValueFromXML('C:\dnpr\ERPBO\Settings.xml',
  '//settings/Mail/UserName');
EditMailServerPassword.Text := LoadValueFromXML('C:\dnpr\ERPBO\Settings.xml',
  '//settings/Mail/Password');
EditERPBOServicePath.Text := LoadValueFromXML('C:\dnpr\ERPBO\Settings.xml',
  '//settings/default/ERPBOWebServicePath');
EditERPHOServicePath.Text := LoadValueFromXML('C:\dnpr\ERPBO\Settings.xml',
  '//settings/default/ERPHOWebServicePath');
EditCustomerId.Text := LoadValueFromXML('C:\dnpr\ERPBO\Settings.xml',
  '//settings/license/customernumber');

Upvotes: 4

Related Questions