cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
prabubiginstall
Level 5

how to replace an xml attribute with a new value in installer

thanks pv...

i have another requirement as a part of the installer...

As a part of our project .. we have a java class .. which generates like a productKey by passing some arguments..

i need to copy that key which is a string to an xml file..

i can see an action called perform xsltransform.. but dont know how to replace an atrributes value with the new one

As always .. waiting for a nice solution
Labels (1)
0 Kudos
(2) Replies
TheTraveler
Level 8

Hello,

Install shield has the ability to create COM objects and use them. We use it to modify a web.config file which is XML encoded.


#define ERROR_XML_FILE_CREATION "Setup could not find the web.config file %s"
#define ERROR "Error"
#define ERROR_XML_FILE_STRUCTURE "The web.config file structure is corrupted.%s"
#define ERROR_XML_FILE_LOAD "The web.config file was not loaded.%s"

prototype ModifyWebConfig(BYVAL STRING, BYVAL STRING);

function ModifyWebConfig(strFileName, strError)
OBJECT oDoc, oNode, oNodeList;
NUMBER i, nSize;
STRING strNamedItem;
STRING strValue;
begin
///////////////////////////////////////////////////////////////////////////
// Check to see if the file exists... //
///////////////////////////////////////////////////////////////////////////
if Is(FILE_EXISTS, strFileName) = TRUE then
//MessageBox("XML file exists",0);
else
MessageBox("XML file is missing",0);
endif;

///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
nSize = 300;
i = 0;
strValue = "Data Source=" + m_strDBDataSource;
strValue = strValue +";User Id=" + m_strDBUserName;
strValue = strValue +";Password=" + m_strDBPassword;
strValue = strValue +";Initial Catalog="+ m_strDBName;
strValue = strValue +";Persist Security Info=True";
if m_strADOConnectionTimeout != "" then
strValue = strValue +";Connection Timeout="+ m_strADOConnectionTimeout;
else
strValue = strValue +";Connection Timeout="+ SQL_CONNECTION_TIMEOUT;
endif;

///////////////////////////////////////////////////////////////////////////
// get values from public properties
///////////////////////////////////////////////////////////////////////////
set oDoc = CreateObject("Msxml2.DOMDocument.4.0");
if (IsObject(oDoc) = FALSE) then
MessageBox(ERROR_XML_FILE_CREATION, 0);
return -1;
endif;
oDoc.async = FALSE;
oDoc.setProperty("SelectionLanguage", "XPath");

///////////////////////////////////////////////////////////////////////////
// if success, traverse file and substitute value //
///////////////////////////////////////////////////////////////////////////
if oDoc.load(strFileName) then
///////////////////////////////////////////////////////////////////////
// get list of matching nodes //
///////////////////////////////////////////////////////////////////////
set oNodeList = oDoc.getElementsByTagName("*");
if (oNodeList.length > 0) then
for i = 0 to (oNodeList.length - 1);
set oNode = oNodeList.nextNode;
try
strNamedItem = oNode.attributes.getNamedItem("key").value;
catch
strNamedItem = "";
endcatch;

try
if strNamedItem = "SQLConnection" then
oNode.attributes.getNamedItem("value").value = strValue;
endif;
catch
endcatch;
endfor;
else
MessageBox(ERROR_XML_FILE_STRUCTURE,SEVERE);
return -1;
endif;
else
MessageBox(ERROR_XML_FILE_LOAD,SEVERE);
return -1;
endif;

oDoc.save(strFileName);
set oDoc = NOTHING;
end;
0 Kudos
mberube
Level 2

You could also use a specific string (like MY_PRODUCT_KEY) inside the .xml file and use the 'Modify Text File - Single File' action to replace the content of this string with a variable containing your product key. It's simpler but it might do the job for you.

To set this up, click the 'Configure...' button near the 'Search and replace strings' in the action

If you prefer java to c you can also write a Custom Action instead of a COM object.
0 Kudos