Data Source -- Read a file by Custom (Load by Script)

Vincenzo Basile
Tera Expert
I all,
I have the necessity to import an XML File that store a complex data structure,
Here an Example: 
 

 

 

<c SEGMENT="1"> 
<a> val_A1</a>
<d> val_D1</d>
</c>
<c SEGMENT="2"> 
<a> val_A2</a>
<b> val_B2</b>
</c>
<c SEGMENT="3"> 
<a> val_A3</a>
</c>
<b> val_D</d>

 

 

 
my target is to have 3 import set rows all with 4 columns
 
val_A1 ;   null   ;  null   ; val_D1
val_A2 ;val_B2;  null   ; val_D
val_A3 ;   null  ;  null   ; val_D
 
Sing the standard approch, so using Type = File and Format = XML with the XPath capability, I'm not bale to have this result...
 
So my idea is to use a script into the "data source" that will execute the xml parsing, and so that will be generate the corret row into the import_set_table.
 
The problem that I discover now is that:
Using the "Custom Load by Script", available on TYPE field; I don't know how read and so read the file attached to the record...
Using the "Custom Parse by Script", avilable on FORMAT field; the file is readed line by line and so I'n not able to use all the data in the "XMLHelper" library 
 
Could someone have a solution to this problem ?
 
Best Regards 
Vincenzo
1 ACCEPTED SOLUTION

Vincenzo Basile
Tera Expert

 

Hi All,

I have fouond a solution to the reported problem,


I used a Data Source with Type "Custom (Load by Script)", using the Data Loader Script I read the attached XML file and transform it in a JSON form,

now I'm able the design a code that will extrad into the Import set only the colum that I need

 

here the code:

(function loadData(import_set_table, data_source, import_log, last_success_import_time) {

	
    var attachment = new GlideSysAttachment();
    var agr = attachment.getAttachments(data_source.getTableName(), data_source.getUniqueValue());
    while (agr.next()) {
        var agrValue = attachment.getContentStream(agr.getUniqueValue());
        var reader = new GlideTextReader(agrValue);
		var txtFull = '';
		do{
			var txtRow = reader.readLine();
			if (txtRow){
				txtFull+=txtRow;
			}
		}while (txtRow != null);
		gs.log('Fulltxt = ' +txtFull, 'DataSourceLoader');

		var helper = new XMLHelper(txtFull);
		var json_obj = helper.toObject();
		gs.log('JSON value = ' + JSON.stringify(json_obj,null,3), 'DataSourceLoader');

    }

})(import_set_table, data_source, import_log, last_success_import_time);

 

Regards

Vincenzo

View solution in original post

1 REPLY 1

Vincenzo Basile
Tera Expert

 

Hi All,

I have fouond a solution to the reported problem,


I used a Data Source with Type "Custom (Load by Script)", using the Data Loader Script I read the attached XML file and transform it in a JSON form,

now I'm able the design a code that will extrad into the Import set only the colum that I need

 

here the code:

(function loadData(import_set_table, data_source, import_log, last_success_import_time) {

	
    var attachment = new GlideSysAttachment();
    var agr = attachment.getAttachments(data_source.getTableName(), data_source.getUniqueValue());
    while (agr.next()) {
        var agrValue = attachment.getContentStream(agr.getUniqueValue());
        var reader = new GlideTextReader(agrValue);
		var txtFull = '';
		do{
			var txtRow = reader.readLine();
			if (txtRow){
				txtFull+=txtRow;
			}
		}while (txtRow != null);
		gs.log('Fulltxt = ' +txtFull, 'DataSourceLoader');

		var helper = new XMLHelper(txtFull);
		var json_obj = helper.toObject();
		gs.log('JSON value = ' + JSON.stringify(json_obj,null,3), 'DataSourceLoader');

    }

})(import_set_table, data_source, import_log, last_success_import_time);

 

Regards

Vincenzo