Book a Demo

Author Topic: Importing XML files using JavaScript  (Read 7629 times)

Dean Moor

  • EA Novice
  • *
  • Posts: 4
  • Karma: +0/-0
    • View Profile
Importing XML files using JavaScript
« on: April 12, 2022, 08:33:24 pm »
Team,

I am trying to create a script that imports data from a collection of XML files as elements. While I can easily get this working for a single file, I am attempting to get the files from a folder dynamically with the FileSystem Object. This is a piece of cake in VBScript (However, the lack of a debugging environment issue makes this language less than desirable for the large translation scripting!) but can not seem to find a way to iterate through the IFileCollection in JavaScript. Can anyone point me in the right direction?

Code: [Select]
const glDirectory = '\\\\SomeDrive\\SomeFolder\\';
var lcFileList = new Array();
var fsObject = new COMObject( "Scripting.FileSystemObject" );

if (fsObject.FolderExists(glDirectory)){
var fsFolder = fsObject.GetFolder(glDirectory);
if (fsFolder){
var fsFileCollection = fsFolder.Files;
if (fsFileCollection){
//Itterating File Collection & adding to my array for later on.
for (i=0; i<fsFileCollection.Count; i++)
{
var file = fsFolder.Files[i];  //This does not return anything!
lcFileList.push(file); //This errors as file is undefined (aka, not retrieved from the collection.
}
}
}
}

Your help is greatly appreciated.

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Importing XML files using JavaScript
« Reply #1 on: April 13, 2022, 01:11:18 am »
You can debug vbscript if you have install the microsoft script debugger.

I have a copy of that on my google drive as it is not available anymore from Microsoft
https://drive.google.com/file/d/0B5YX31GyMA64NUJsQnJiaVhLWlk/view?usp=sharing&resourcekey=0-hnwq41cEfiaQT9BTYW325Q

Geert

Dean Moor

  • EA Novice
  • *
  • Posts: 4
  • Karma: +0/-0
    • View Profile
Re: Importing XML files using JavaScript
« Reply #2 on: April 13, 2022, 03:04:30 am »
Thanks for your quick response Geert.
Sadly, your link results in a 404.

In addition, while this is one option that may be available to some, this is not an option for me. The environment is very tightly controlled and obtaining permission to installing additional development tools is extremely unlikely. The best possible solution (if possible) is to leverage Javascript.

Simply put, this script needs to be supportable by others as well, and while an exception could be made for one machine to have additional tools installed, it is far less likely for an entire team.

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Importing XML files using JavaScript
« Reply #3 on: April 13, 2022, 04:03:40 am »
I see. The link works (I just doublechecked), but it is probably blocked in your environment.

I'm sure you can do the same thing with Javascript, but I'm not able to help you with that.

Geert

Dean Moor

  • EA Novice
  • *
  • Posts: 4
  • Karma: +0/-0
    • View Profile
Re: Importing XML files using JavaScript
« Reply #4 on: April 13, 2022, 04:19:14 am »
Thanks anyway Geert, I appreciate the help.

With the code I posted, I can obtain a collection of files showing the file count (which I can confirm matches the directory when changed). Unfortunately, I have not found a way to obtain the individual files from that collection. In Javascript, the collection is not iterable, and every trick I know fails to resolve this state.

Sunshine

  • EA Practitioner
  • ***
  • Posts: 1353
  • Karma: +121/-10
  • Its the results that count
    • View Profile
Re: Importing XML files using JavaScript
« Reply #5 on: April 13, 2022, 05:09:59 pm »

Code: [Select]
const glDirectory = '\\\\SomeDrive\\SomeFolder\\';
var lcFileList = new Array();
var fsObject = new COMObject( "Scripting.FileSystemObject" );

if (fsObject.FolderExists(glDirectory)){
var fsFolder = fsObject.GetFolder(glDirectory);
if (fsFolder){
var fsFileCollection = fsFolder.Files;
if (fsFileCollection){
//Itterating File Collection & adding to my array for later on.
for (i=0; i<fsFileCollection.Count; i++)
{
var file = fsFolder.Files[i];  //This does not return anything!
lcFileList.push(file); //This errors as file is undefined (aka, not retrieved from the collection.
}
}
}
}


Have thought about using an enumerator to navigate the files collection.
Unfortunately there isn't an enumerator in JavaScript supplied with Sparx EA but there is in JScript.
Here is code for a JavaScript Enumerator that I found when I was converting my JScript to JavaScript code.

Code: [Select]
!INC JavaScriptLib.JavaScript-EAConstants
/*
Set of Enumerator functions to support iterating through collections
*/
function moveNext()
{
if(this.iElem > -1)
{
this.iElem++;
if(this.iElem < this.Package.Count)
{
return true;
}
this.iElem = this.Package.Count;
}
return false;
}

function item()
{
if( this.iElem > -1 && this.iElem < this.Package.Count)
{
return this.Package.GetAt(this.iElem);
}
return null;
}

function atEnd()
{
if((this.iElem > -1) && (this.iElem < this.Package.Count))
{
return false;
}
// Session.Output("at end!");
return true;
}

function Check( obj)
{
if(obj == undefined)
{
Session.Output("Undefined object");
return false;
}
return true;
}

function Enumerator( object )
{
this.iElem = 0;
this.Package = object;
this.atEnd = atEnd;
this.moveNext = moveNext;
this.item = item;
this.Check = Check;
if(!Check(object))
{
this.iElem = -1;
}
}
Happy to help
:)

lacsap

  • EA Novice
  • *
  • Posts: 1
  • Karma: +0/-0
    • View Profile
Re: Importing XML files using JavaScript
« Reply #6 on: October 10, 2022, 07:16:51 pm »
Hi!

Did you find any solution to this?

I have copied your Javascript code and think I have come slightly further. I think you should use the IFileCollection.Item function to retrieve elements from the collection. However, I have tried both 0 and 1 indeces without success..

Thanks


Guillaume

  • EA Practitioner
  • ***
  • Posts: 1403
  • Karma: +42/-2
    • View Profile
    • www.umlchannel.com
Re: Importing XML files using JavaScript
« Reply #7 on: October 19, 2022, 12:25:15 am »
You can debug vbscript if you have install the microsoft script debugger.

I have a copy of that on my google drive as it is not available anymore from Microsoft
https://drive.google.com/file/d/0B5YX31GyMA64NUJsQnJiaVhLWlk/view?usp=sharing&resourcekey=0-hnwq41cEfiaQT9BTYW325Q

Geert

Hi Geert,

I managed to download and install the debugger which works with EA VBScripts.
However I cannot see the properties of an object e.g. if I have an EA.attribute variable, I cannot see attr.Name, attr.AttributeGUID in the Locals view.
Am I doing something wrong ?
Guillaume

Blog: www.umlchannel.com | Free utilities addin: www.eautils.com


Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Importing XML files using JavaScript
« Reply #8 on: October 19, 2022, 12:42:56 am »
Not sure, for me it works:


https://imgur.com/a/O76TZsd

Geert

Guillaume

  • EA Practitioner
  • ***
  • Posts: 1403
  • Karma: +42/-2
    • View Profile
    • www.umlchannel.com
Re: Importing XML files using JavaScript
« Reply #9 on: October 19, 2022, 05:33:42 pm »
I found the issue: I have several EA versions installed on my PC (EA 15.1, 15.2, EA 16) and it only works with the latest version.

Guillaume

Blog: www.umlchannel.com | Free utilities addin: www.eautils.com