Monday, January 24, 2011

Read Write XML data

In Installshield projects, often we need to manipulate XML, either by reading specific values, or updating some values. For e.g. in case of configs. The examples mentioned below will be very handy when dealing with XML's.
Reading XML data:
// Creating an object for MsXML2 for xml manipulation
set oDoc = CoCreateObject("Msxml2.DOMDocument");
oDoc.setProperty("SelectionLanguage", "XPath");


// load the XML document into memory
if oDoc.load(strFileName) then


// Set XPath in XMl file to work on...
szXPath = "/configurations/setting[@name='AuthenticationAuthService']";


// This will return the default value for AuthenticationAuthService
sAuthServerURL = ReadDefaultConfigValues(szXPath,"default",oDoc);


Function: The below function is a custom action which will return the values from the XML tag elements.


function STRING ReadDefaultConfigValues(szXPath,szkey,oDoc)
OBJECT oNode;
begin
set oNode = oDoc.documentElement.selectSingleNode(szXPath);
if(gbConfigImported==FALSE) then
return oNode.attributes.getNamedItem(szkey).value;
else
return oNode.text;
endif;
end;
-------------------------------
Writing/saving XML data
set oDoc = CoCreateObject("Msxml2.DOMDocument");
oDoc.setProperty("SelectionLanguage", "XPath");


// set up variable with fullpath to xml config file
szConfigFile = gWIN_INSTALLDIR^"PaginationService\\TranscendBTG4.PaginationService.exe.config";


Configure_ConfigFile(szConfigFile,"/configuration/connectionStrings/add[@name='TranscendConnectionString']",strConnectionString,"connectionString",oDoc);


// Save the XML Changes
oDoc.save(szConfigFile);


Function: The below function is a custom action which will save/update the values from the XML tag elements.


function Configure_ConfigFile(szConfigFile,szXPath,strValue,key,oDoc)
OBJECT oNode;
begin
// load the XML document into memory
if oDoc.load(szConfigFile) then
//Set the XPath in the XML
set oNode = oDoc.documentElement.selectSingleNode(szXPath);
oNode.attributes.getNamedItem(key).value = strValue;
endif;
end;