Loop through XML, find assets

Post Reply
StoereSjoerd
Newbie
Posts: 8
Joined: Thu Oct 29, 2015 3:51 pm

Loop through XML, find assets

Post by StoereSjoerd »

Hi,

I have the following assets stored into a folder:

Asset-1.pdf
Asset-2.pdf
Asset-3.pdf

Within a XML there are nodes with values regarding to these assets:

Code: Select all

<FileName>Asset-1.pdf</FileName>
<FileName>Asset-2.pdf</FileName>
<FileName>Asset-3.pdf</FileName>
So I want to collect these assets and archive them based on the data from the XML. How can this be accomplished? I think scripting is necessary in this case.

Regards, Sjoerd
Last edited by StoereSjoerd on Fri Feb 05, 2016 9:20 am, edited 1 time in total.
User avatar
gabrielp
Advanced member
Posts: 577
Joined: Fri Aug 08, 2014 4:31 pm
Location: Boston
Contact:

Re: Loop through XML, find assets

Post by gabrielp »

You could use a script for this but maybe give this a shot http://forum.enfocus.com/viewtopic.php?f=12&t=1201
Chat: open-automation @ gitter
Code: open-automation & dominickp @ GitHub
Tools: Switch, Pitstop, EPMS, Veracore, PageDNA, SmartStream, Metrix
StoereSjoerd
Newbie
Posts: 8
Joined: Thu Oct 29, 2015 3:51 pm

Re: Loop through XML, find assets

Post by StoereSjoerd »

Thank you for your quick response.

But splitting the XML is losing the coherence of the assets. I need to combine these assets into one job for archiving it afterwards. I think the 'asssemble job' configurator needs to do the job. But my script which is looping through the XML finding the assets, does not do the desired job:

Code: Select all

var myAssets = job.getVariableAsString('[Metadata.Text:Path="/Order/Assets/FileName",Dataset="Collect",Model="XML"]');

for (var i=0; i<myAssets.length; i++) {

	myAssets[i];

}
loicaigon
Member
Posts: 147
Joined: Wed Jul 10, 2013 10:22 am

Re: Loop through XML, find assets

Post by loicaigon »

Hi,

I am guessing your issues is that your presume myAssets is an array like. Infact, it's a String made of the first occurence of FileName.
When you loop through myAssets length, you are actually working with every single character of the string. If myAssets is "a.pdf", you will process "a", then"." then "p" then "d" then "f".

Here is my proposal, rather than looking into the metadata and given that the xml file is the job, I suggest to load a document and look through nodes :

Code: Select all

// Is invoked each time a new job arrives in one of the input folders for the flow element.
// The newly arrived job is passed as the second parameter.
function jobArrived( s : Switch, job : Job )
{
	var d = new Document ( job.getPath() );
	var f, url, j, displaced = 0;
	
	var elems = d.evalToNodes("/Order/Assets/FileName");
	var n = elems.getCount( );
	while (n--) {
		url = "/Users/ozalto/Desktop/"+elems.getItem(n).evalToString(".");
		if ( File.exists ( url ) ) {
			s.log(-1,"File found" );
			f = new File ( url );
			j = s.createNewJob(url);
			j.sendToSingle(url);
			displaced++;
		}
		else {
			s.log(3, "File not found "+url );
		}
	}
	
	if ( displaced == n ) job.sendToNull();
	else job.fail();
}
HTH,

Loic
http://www.ozalto.com
StoereSjoerd
Newbie
Posts: 8
Joined: Thu Oct 29, 2015 3:51 pm

Re: Loop through XML, find assets

Post by StoereSjoerd »

Hi Loic,

Thank you for your example. This script is picking up the assets regarding the XML indeed. But how to collect these assets to one job or archive file?

Regards, Sjoerd
StoereSjoerd
Newbie
Posts: 8
Joined: Thu Oct 29, 2015 3:51 pm

Re: Loop through XML, find assets

Post by StoereSjoerd »

The following script is sending all assets separtly to the connected folder:

Code: Select all

// Is invoked each time a new job arrives in one of the input folders for the flow element.
// The newly arrived job is passed as the second parameter.
function jobArrived( s : Switch, job : Job )
{
	var theXML = new Document(job.getPath());
	var nodeList = theXML.evalToNodes("/Order/Versions/Version1/Assets/FileName");

	for (var i=0; i < nodeList.length; i++) {

	  var theNode = nodeList.getItem(i);
	  var theAssets = "/Users/Administrator/Desktop/Test Flows/Meerdere/Assets/"+theNode.evalToString(".");	  

	  job.log(1, "Found assets: "+theNode.evalToString("."));
	  
	  job = s.createNewJob(theAssets);

	  job.sendToSingle(theAssets);

	}
}
Now I want to collect these and store them within a folder for archiving it into one zip file afterwards. I tried following script with no luck

Code: Select all

// Is invoked each time a new job arrives in one of the input folders for the flow element.
// The newly arrived job is passed as the second parameter.
function jobArrived( s : Switch, job : Job )
{
	var theXML = new Document(job.getPath());
	var nodeList = theXML.evalToNodes("/Order/Versions/Version1/Assets/FileName");
	
	var theDir = new Dir( "/Storage/" );
	var theTempPath = s.createPathWithName(theDir);
	
	job = s.createNewJob(theTempPath);

	for (var i=0; i < nodeList.length; i++) {

	  var theNode = nodeList.getItem(i);
	  var theAssets = "/Users/Administrator/Desktop/Test Flows/Meerdere/Assets/"+theNode.evalToString(".");	  

	  job.log(1, "Found assets: "+theNode.evalToString("."));

	  job.sendToSingle(theTempPath, theAssets);

	}
}
This script isn't sendig files to connected folder at all. How can this be accomplished?
User avatar
gabrielp
Advanced member
Posts: 577
Joined: Fri Aug 08, 2014 4:31 pm
Location: Boston
Contact:

Re: Loop through XML, find assets

Post by gabrielp »

I'm a little confused about your code, so take what I'll say with a grain of salt. But a couple issues I'm noticing here. You're overwriting your job variable. Switch gives you "job" which is it's Job class instantiated with the current job which triggered jobArrived(), so overwriting that is probably not what you want to do.

Secondly, once you have a new job, what are you doing to it? Usually when you create a new job, you get a writable path with something like job.createPathWithName which returns a path you can write to, once complete, sending the job out with a sendTo function.
Chat: open-automation @ gitter
Code: open-automation & dominickp @ GitHub
Tools: Switch, Pitstop, EPMS, Veracore, PageDNA, SmartStream, Metrix
Post Reply