1

My question is based on How to read and write XML document node values?. I have an XML file and I am trying to read a value of one of the node. But I am getting

An error occured!

Variant is null, cannot invoke

. The method I used to Load the value from the XML file is below.

function LoadValueFromXML(const AFileName, APath: string): string;
var
  XMLNode: Variant;
  XMLDocument: Variant;  
begin
  Result := '';
  XMLDocument := CreateOleObject('Msxml2.DOMDocument.6.0');
  try
    XMLDocument.async := False;
    MsgBox(AFileName, mbConfirmation, MB_OK);
    MsgBox(APath, mbConfirmation, MB_OK);
    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);
      if VarIsNull(XMLNode) then begin
          MsgBox('Variant is null', mbInformation, MB_OK);//not getting this message 
      end;
      Result := XMLNode.text;
    end;
  except
    MsgBox('An error occured!' + #13#10 + GetExceptionMessage, mbError, MB_OK);
  end;
end;

The XML file snippet is mentioned below.

<?xml version="1.0" encoding="UTF-8"?>
<application xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/application_7.xsd" version="7" id="Test-app">
  <application-name>Test-app</application-name>
  <description>Enterprise Archive to be deployed at web-application server.</description>
  <display-name>TestApp</display-name>
  <module>
    <web>

I have called the function as follows LoadValueFromXML(applicationxmlFile, '//application/application-name'); where applicationxmlFile is a String which holds the filename with path. I am trying to get the application-name node value.

I am using Inno setup compiler 6.0.3.

UPDATE:

When I use Inno Setup Compiler 5.5.9(a) I am getting below error. This is the version of Inno compiler I will use to compile my programs.

An error occured!

Nil interface exception

UPDATE ON ANSWER:

Changed the Procedure as mentioned below:

function LoadValueFromXML(const AFileName, APath, AAttribute: string): string;
var
  XMLNode: Variant;
  XMLDocument: Variant;  
begin
  Result := '';
  XMLDocument := CreateOleObject('Msxml2.DOMDocument');
  try
    XMLDocument.async := False;
    LOG(AFileName);
    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('SelectionNamespaces', 'xmlns:ns=''http://xmlns.jcp.org/xml/ns/javaee''');
      XMLNode := XMLDocument.selectSingleNode(APath);
      MsgBox(XMLNode.text, mbConfirmation, MB_OK);
      Result := XMLNode.text;
    end;
  except
    MsgBox('An error occured!' + #13#10 + GetExceptionMessage, mbError, MB_OK);
  end;
end;

and called LoadValueFromXML(applicationxmlFile, '//ns:application-name', 'application-name'); and now it works like a charm.

Thanks in advance,

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
DeeJay007
  • 469
  • 4
  • 30

0 Answers0