Hi Experts,
I have a bunch of XML files in a folder. What I'd like to achieve is that, I'd like to iteratively load the xml using a StructuredApplication and save it as an FM file. I've been able to sucessfully load the XML file but I'm having difficulty in saving it as an FM file.
Here is the code that I've written so far:
if (sourceFolder != null){ // If a valid folder is selected
files = new Array();
fileType = "*.xml";
files = sourceFolder.getFiles(fileType); // Get all files matching the pattern
if (files.length > 0) { // Get the destination to save the files
destFolder = Folder.selectDialog( 'Select the folder where you want to save the converted XML files.', '~' );
for (var i = 0; i < files.length; i++) {
// Set the options for opening the XML file.
var openParams = GetOpenDefaultParams();
var j = GetPropIndex(openParams, Constants.FS_OpenAsType);
openParams[j].propVal.ival = Constants.FV_TYPE_XML;
j = GetPropIndex(openParams, Constants.FS_StructuredOpenApplication);
openParams[j].propVal.sval = "XML snpsbook";
j = GetPropIndex(openParams, Constants.FS_FileIsOldVersion);
openParams[j].propVal.ival = Constants.FV_DoOK;
j = GetPropIndex(openParams, Constants.FS_FontNotFoundInDoc);
openParams[j].propVal.ival = Constants.FV_DoOK;
j = GetPropIndex(openParams, Constants.FS_FileIsInUse);
openParams[j].propVal.ival = Constants.FV_DoCancel;
j = GetPropIndex(openParams, Constants.FS_AlertUserAboutFailure);
openParams[j].propVal.ival = Constants.FV_DoCancel;
var returnParams = new PropVals();
// I'M NOT ABLE TO GET THIS LINE TO EXECUTE. sourceDoc RETURNS AN INVALID OBJECT REFERENCE.
sourceDoc = Open(files[i].name, openParams, returnParams); // Return the document object
alert(sourceDoc.name);
// Function to save .XML file to .FM extension
var saveParams = GetSaveDefaultParams();
var returnParamsp = new PropVals();
// Replace the .xml extension with .fm
var k = GetPropIndex(saveParams, Constants.FS_FileType);
saveParams[k].propVal.ival = Constants.FV_SaveFmtBinary;
saveAsName = sourceDoc.name.replace (/\.[^\.\\]+$/,".fm");
var saveInFile = Save(saveAsName, saveParams, returnParamsp); // Save as .fm file
saveInFile = new File ( destFolder + '/' + targetFile);
saveInFile.close(); // Close file
}
alert('Files are saved as FM in ' + destFolder);
} else {
alert('No matching files found!!!');
}
}